Problem accessing one class from another and vice versa
-
Hello, I am writing a application in Visual C++ .NET 7.0. I'd like to keep my classes in seperate files to keep things organized, but I am having difficulty accessing classes from within two files. Can someone help me find a solution accessing between files in this manner? I need to access methods and data back and forth in this manner, but I don't want the two classes to be in the same file. What am I doing wrong? Is there a different way to do this? Here is a simplified version: Class1.h #ifndef __Class1H__ #define __Class1H__ #include "Class2.h" class Class1 { public: Class1(); ~Class1(); //This doesn't work because it is in a different file! Class1(Class2* object); //This doesn't work because it is in a different file! Class2 obj; }; #endif // __Class1H__ Class2.h #ifndef __Class2H__ #define __Class2H__ //this is causing problems... #include "Class1.h" class Class2 { public: Class2(); ~Class2(); }; #endif // __Class2H__ Class1.cpp #include "Class1.h" Class1::Class1() { } Class1::~Class1() { } Class2.cpp #include "Class2.h" Class2::Class2() { } Class2::~Class2() { }
-
Hello, I am writing a application in Visual C++ .NET 7.0. I'd like to keep my classes in seperate files to keep things organized, but I am having difficulty accessing classes from within two files. Can someone help me find a solution accessing between files in this manner? I need to access methods and data back and forth in this manner, but I don't want the two classes to be in the same file. What am I doing wrong? Is there a different way to do this? Here is a simplified version: Class1.h #ifndef __Class1H__ #define __Class1H__ #include "Class2.h" class Class1 { public: Class1(); ~Class1(); //This doesn't work because it is in a different file! Class1(Class2* object); //This doesn't work because it is in a different file! Class2 obj; }; #endif // __Class1H__ Class2.h #ifndef __Class2H__ #define __Class2H__ //this is causing problems... #include "Class1.h" class Class2 { public: Class2(); ~Class2(); }; #endif // __Class2H__ Class1.cpp #include "Class1.h" Class1::Class1() { } Class1::~Class1() { } Class2.cpp #include "Class2.h" Class2::Class2() { } Class2::~Class2() { }
first, why do you call #include "Class1.h" into Class2.h when Class2 dont use any object from Classe 1 ?? So, do this :
Class1.h
#include "Class2.h";class Class1;
class Class1 {
Class2 MyClass2Object;
//...
}Class2.h
class Class2;
class Class2 {
//...
}there won't have any recursive call problem anymore... i forgot to say, don't put the data members into
public:
statement... prefer this for the interface (member functions which are allowed for the users).
TOXCCT >>> GEII power
-
Hello, I am writing a application in Visual C++ .NET 7.0. I'd like to keep my classes in seperate files to keep things organized, but I am having difficulty accessing classes from within two files. Can someone help me find a solution accessing between files in this manner? I need to access methods and data back and forth in this manner, but I don't want the two classes to be in the same file. What am I doing wrong? Is there a different way to do this? Here is a simplified version: Class1.h #ifndef __Class1H__ #define __Class1H__ #include "Class2.h" class Class1 { public: Class1(); ~Class1(); //This doesn't work because it is in a different file! Class1(Class2* object); //This doesn't work because it is in a different file! Class2 obj; }; #endif // __Class1H__ Class2.h #ifndef __Class2H__ #define __Class2H__ //this is causing problems... #include "Class1.h" class Class2 { public: Class2(); ~Class2(); }; #endif // __Class2H__ Class1.cpp #include "Class1.h" Class1::Class1() { } Class1::~Class1() { } Class2.cpp #include "Class2.h" Class2::Class2() { } Class2::~Class2() { }
oh, i see another problem... each header file must be "included" once into the project. for that, you must use preprocessor directive to exclude recursive redifinitions :
Class1.h
#if !defined(__CLASS1_H_INCLUDED__)
#define __CLASS1_H_INCLUDED__//...
#endif//__CLASS1_H_INCLUDED__
Class2.h
#if !defined(__CLASS2_H_INCLUDED__)
#define __CLASS2_H_INCLUDED__//...
#endif//__CLASS2_H_INCLUDED__
that concern only .h files. don't forget !
TOXCCT >>> GEII power