-2

I am making a novice level text based game. I want to be able to make a file with variables that read out some text. It is for the game guide. You have 3 classes you can choose from and I want to make a file for each one that would:Set levels, etc. And being able to program the levels are not the problem.

I would like to know how to import one of the .cpp files that i have in the same folder so that I could call upon a variable from that file in another file.

Eg: In my main.cpp i would like to call upon magic.cpp that is in the src class.

Why i want this:

Instead lf having 10,000 lines of code in one .cpp I would like to be able to link them.

What it will do for me:

While my games master magician is explaining the game, each class will have a different explanation. So if the player chose to be a magician i could call from the magician.cpp to give correct items, levels, and some text about being a magician inside my game.

Really hope i was descriptive enough.

Thank you, Nate.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • Why don't you just split the code into logical .cpp files, compile and link them together (as opposed to #including them into a single file, which is really just a convoluted way of making a 10,000 line file)? – Scott Hunter Jan 15 '15 at 01:30
  • Well i suppose that's kinda what i want to do. I am pretty new to cpp and programming logic in general. I would jist like to #include a file from my src folder and make all the variables and content usable in another cpp file. Its just organization issue for me. – Nate Fryman Jan 15 '15 at 01:34
  • If you are trying to include one cpp file in another, you have designed something poorly. That is where you should be using functions, inheritance, or something else to reuse your code. Do you know how to write a program that uses more than one cpp file? It also sounds like you are confusing the C++ class with a game "class" . – mattm Jan 15 '15 at 01:34
  • 1
    This is an [X Y Problem](http://meta.stackexchange.com/questions/66377). You're really seeking advice on how to separate your code into smaller source files. Your question about how to include cpp files is a pursuit of the wrong answer. – Drew Dormann Jan 15 '15 at 02:15
  • really,just a simple search: https://www.google.com/?gws_rd=ssl#q=c%2B%2B+multiple+files – bolov Jan 15 '15 at 02:16
  • possible duplicate of [Using multiple .cpp files in c++ program?](http://stackoverflow.com/questions/6995572/using-multiple-cpp-files-in-c-program) – bolov Jan 15 '15 at 02:17

2 Answers2

0

It totally depends on the compiler you're using. For instance if you're using g++, you can use the following command to produce the output file:

g++ MainMethodFile.cpp SecondFile.cpp ThirdFile.cpp -o Output

And if you're using visual studio then it will do this automatically whenever you compile your program.

Waseem Hassan
  • 233
  • 2
  • 10
  • Well im using CodeBlocks. And im very new to all of this. So im not 100% sure what compiler im using. – Nate Fryman Jan 15 '15 at 01:37
  • Then simply add new .cpp files and put your code inside. CodeBlocks will automatically link them for you at compile time. – Waseem Hassan Jan 15 '15 at 01:38
  • Yes, i tried that. But whenever i would call upon a variable from the other cpp file, it would say it hasnt been defined in this scope, and i was also get the error that int main () was already in use – Nate Fryman Jan 15 '15 at 01:40
  • Here's what you need to do: 1. Create a C++ Project. 2. Add 2 CPP Files 3. You need to put int main() function in only 1 file. 4. Create a global variable in the other file and try to access it in main() function. It should work. – Waseem Hassan Jan 15 '15 at 01:41
  • I will try it. Thank you for your patience and help! – Nate Fryman Jan 15 '15 at 01:43
  • Also does it make a difference that the c++ project is a console application? Should it be a shared library? – Nate Fryman Jan 15 '15 at 01:45
  • It should be a console application at this point. Using a shared library is not that easy, and it may lead to confusions to new developers. – Waseem Hassan Jan 15 '15 at 01:57
  • Thank you so much, i finally figured it out haha. Im just new to all this and some things are a bit confusing. – Nate Fryman Jan 15 '15 at 03:38
  • Glad to hear that :) Kindly mark my answer as right one and up it. Thanks – Waseem Hassan Jan 15 '15 at 08:38
0

You can't include .cpp files, you can include only .h files. Generally, you shoud put definitions of classes and declarations for functions and variables you want to use in other .cpp files in header files and definitions of class methods or functions in .cpp files. That is a way to properly orgranize your code, here is an example:

file1.h:

class C1 {
    //...
    void method1();
    //...
}

file1.cpp:

#include "file1.h"

//...

void C1::method1() {
//...
}

file2.h:

class C2 {
    //...
    void method2();
    //...
}

file2.cpp:

#include "file2.h"

//...

void C2::method2() {
//...
}

main.cpp:

#include "file1.h"
#include "file2.h"

int main {
    // You can use classes C1 and C2 here
}
vasicbre
  • 96
  • 8