I am making a program (a text game) and I am using four different classes in it. I using a different file for each class. What I want to know is, how do I run/compile the classes together? If it helps, I am running Dev-C++
Thanks for the help.
I am making a program (a text game) and I am using four different classes in it. I using a different file for each class. What I want to know is, how do I run/compile the classes together? If it helps, I am running Dev-C++
Thanks for the help.
Assuming that all the files are part of one project, you would just build the project and Dev-C++ will compile each file and link them together for you.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
When I want to use the class while in another class, I do: Class classname; then I do classname.method();
When I do the Class classname; I get an error. What do I need to do?
Okay, when you have classes A, B, and C for example. Each class needs two files a header file for each, and an implementation file for each, so it will be
"A.h", "B.h", "C.h" and for implementation "A.cpp", "B.cpp", and "C.cpp"
In the header file of A, if you would like to use either B or C, you have to put at the top
Also to make sure no multiple inclusion errors occur, put your code in the implementation files in this wayCode:#include "C.h"
Replacing A_H_GUARD with B_H_GUARD, or C_H_GUARD depending on the file. This way if you want to include C into a and B you don't have to worry about multiple inclusion compile complaints.Code:#ifndef A_H_GUARD #define A_H_GUARD //your A.h declarations go here #endif
OK, this is what I have as a header:
I get an error saying "GameManager.h: No such file or directory"Code:#include <iostream.h> using namespace std; #ifndef GameManager_H_GUARD #define GameManager_H_GUARD #include "GameManager.h" #endif
Yes, there is a GameManager.cpp file.
Edit: Do I need to have the
in every class file?Code:#include <iostream.h> using namespace std;
<iostream.h> is outdated
<iostream> should be used
using namespace std; should never occur in the header
is GameManager.h is located in the same directory as the file that includes it?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Yes, well, the file isn't called GameManager.h it's GameManger.cpp, don't know if that matters or not.
you should not include cpp-files... So what exactly are you doing?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I have four classes and I made each one it's own .cpp file and they are all in the same project...
Another thing, I saw in a tutorial that I need to have #include <string> in the header when using string variables, is this true?
You should place them (the class declarations) in header files (.h files in this case), and then implement the classes in .cpp files.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
OK, I made the classes, except the main() class, into .h files.
What do you mean by implement the clesses in .cpp files?
define the functions in the cpp file
a.ha.cppCode:#ifndef A_H_GUARD #define A_H_GUARD class a { public: a(); private: int i; }; #endifCode:#include "a.h" a::a() { i = 0; }
OS: Windows 7, XUbuntu 11.10, Arch Linux
IDE: CodeBlocks
Compiler: GCC
Header (MyClass.h):Source (MyClass.cpp):Code:#ifndef MYCLASS_H #define MYCLASS_H // Any #includes or forward declarations you need go here. class MyClass { public: MyClass(); void MemberFunction(); private: int memberVariable; }; #endifMain (main.cpp):Code:#include "MyClass.h" // Any other #includes you need go here. MyClass::MyClass() : memberVariable(0) { } void MyClass::MemberFunction() { // Do stuff }If you have a second class that uses the first class, then the cpp file should #include both its own header and the other class's header.Code:// Other #includes you need can go here. #include "MyClass.h" // Other #includes you need can go here. int main() { MyClass myVariable; myVariable.MemberFunction(); }
For example:
Code:#ifndef X_H #define X_H // x.h class X { public: void foo(); }; #endifCode:// x.cpp #include "x.h" void X::foo() { // ... }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
OK I'll do that. What about the main class? I can't really do that sort of thing since there's no constructor.
Edit: I'm still getting an error at the
lineCode:#include "GameManager.h"
Last edited by Cypher; 06-20-2007 at 12:33 PM.