Interdependent classes
-
Hi all, Please help me out of this problem. I have 3 classes which all require each other. The problem is that they fail to recognize each other on compilation and give errors as: Undefined class Two, Undefined class Three and Undefined class One Someone suggested me forward declaration. But that too did not help. Where am i going wrong ? Please help me. Here is my code.. (Actually i am having the same problem with an VC++ program. This is just to simulate the same problem) File1.h
class One { private: int one; public: int GetOne(); Two tw; Three th; };
File2.h
class Two { private: int two; public: int GetTheTwo(); One on; Three th; };
File3.hclass Three { private: int three; public: int GetAslThree(); One on; Two tw; };
File1.cpp#ifndef _FILE1_H #define _FILE1_H #include "File1.h" #endif int One::GetOne() { return one; }
File2.cpp#ifndef _FILE2_H #define _FILE2_H #include "File2.h" #endif int Two::GetTheTwo() { return this->two; }
File3.cpp#ifndef _FILE3_H #define _FILE3_H #include "File3.h" #endif int Three::GetAslThree() { return this->three; }
Thanks in advance.*** Who said nothing is impossible? I have been doing it for a long time ***
What Maxwell said is correct, so you can put class Class1; class Class2; at the top of Class3, and include the headers in the CPP, so long as Class3 returns, accepts and stores only pointers to Class1 and Class2. Then the compiler only needs to know that Class1 and Class2 exist, and that it needs to allocate storage for a pointer.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
What Maxwell said is correct, so you can put class Class1; class Class2; at the top of Class3, and include the headers in the CPP, so long as Class3 returns, accepts and stores only pointers to Class1 and Class2. Then the compiler only needs to know that Class1 and Class2 exist, and that it needs to allocate storage for a pointer.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Hi Maxwell/Christian, Thanks a lot guys for the quick reply. That really helped me. If i have a situation where i do need to declare an object rather than a pointer, then is there any possible way for it. Is it possible that i declare the pointer inside the class, and in the constructor of the class, i allocate memory for it. Will, that work. Or is there any other way for it. Since i am having different header files and CPP files, is it possible to include the files in such a way so that the declaration of a class appears before where it is required. But i guess, thats not possible with three classes. Thanks a lot friends..!!!
*** Who said nothing is impossible? I have been doing it for a long time ***
-
Forward declaration can only be used for pointer types because the concrete definitions of the types have not been known yet in the compilation scope.
class One;
class Two;
class Three{
private:
int three;
public:
int GetAslThree();
One* pon;
Two* ptw;
};
Maxwell Chen
Hi Maxwell, Thanks a lot for the quick reply. That really helped me. But i have a situation where i do need to declare an object rather than a pointer, is there any possible way for it? Is it possible that i declare the pointer inside the class, and in the constructor of the class, i allocate memory for it. I am not able to make that work. Is there any other way for it. Thanks in advance !!
*** Who said nothing is impossible? I have been doing it for a long time ***
-
Hi all, Please help me out of this problem. I have 3 classes which all require each other. The problem is that they fail to recognize each other on compilation and give errors as: Undefined class Two, Undefined class Three and Undefined class One Someone suggested me forward declaration. But that too did not help. Where am i going wrong ? Please help me. Here is my code.. (Actually i am having the same problem with an VC++ program. This is just to simulate the same problem) File1.h
class One { private: int one; public: int GetOne(); Two tw; Three th; };
File2.h
class Two { private: int two; public: int GetTheTwo(); One on; Three th; };
File3.hclass Three { private: int three; public: int GetAslThree(); One on; Two tw; };
File1.cpp#ifndef _FILE1_H #define _FILE1_H #include "File1.h" #endif int One::GetOne() { return one; }
File2.cpp#ifndef _FILE2_H #define _FILE2_H #include "File2.h" #endif int Two::GetTheTwo() { return this->two; }
File3.cpp#ifndef _FILE3_H #define _FILE3_H #include "File3.h" #endif int Three::GetAslThree() { return this->three; }
Thanks in advance.*** Who said nothing is impossible? I have been doing it for a long time ***
You really should rethink your design here. Having classes that are inter-dependent is not easy to write, and become impossible to maintain. There are tons of options you can take for making this code cleaner, but I will only provide one here:
class Two; class Three; class One { private: int _One; public: std::tr1::shared_ptr<Two*> _Two; std::tr1::shared_ptr<Three*> _Three; One() : _Two(new Two), _Three(new Three) { } }; class Two { private: int _Two; public: std::tr1::shared_ptr<One*> _One; std::tr1::shared_ptr<Three*> _Three; Two() : _One(new One), _Three(new Three) { } }; class Three { private: int _Three; public: std::tr1::shared_ptr<One*> _One; std::tr1::shared_ptr<Two*> _Two; Three() : _One(new One), _Two(new Two) { } };
The best solution, however, would be to redesign these classes such that they are not inter-dependent.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Hi Maxwell/Christian, Thanks a lot guys for the quick reply. That really helped me. If i have a situation where i do need to declare an object rather than a pointer, then is there any possible way for it. Is it possible that i declare the pointer inside the class, and in the constructor of the class, i allocate memory for it. Will, that work. Or is there any other way for it. Since i am having different header files and CPP files, is it possible to include the files in such a way so that the declaration of a class appears before where it is required. But i guess, thats not possible with three classes. Thanks a lot friends..!!!
*** Who said nothing is impossible? I have been doing it for a long time ***
The only other option, as someone suggested, is a redesign. Think about what makes these classes interdependent. Can you create a class which acts as a proxy between the two ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
You really should rethink your design here. Having classes that are inter-dependent is not easy to write, and become impossible to maintain. There are tons of options you can take for making this code cleaner, but I will only provide one here:
class Two; class Three; class One { private: int _One; public: std::tr1::shared_ptr<Two*> _Two; std::tr1::shared_ptr<Three*> _Three; One() : _Two(new Two), _Three(new Three) { } }; class Two { private: int _Two; public: std::tr1::shared_ptr<One*> _One; std::tr1::shared_ptr<Three*> _Three; Two() : _One(new One), _Three(new Three) { } }; class Three { private: int _Three; public: std::tr1::shared_ptr<One*> _One; std::tr1::shared_ptr<Two*> _Two; Three() : _One(new One), _Two(new Two) { } };
The best solution, however, would be to redesign these classes such that they are not inter-dependent.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Hi, I'm not sure if this will work. I think that this will surely blow your memory into oblivion. When you create an One object it will create a Two and a Three object. Those will create 2 One objects a Three and a Two object. These four objects will create on there turn 3 Two, 3 Three and 2 One objects and so further and so on ... Its like you said very dangerous to desing an inter-dependen class relation !!!
codito ergo sum
-
The only other option, as someone suggested, is a redesign. Think about what makes these classes interdependent. Can you create a class which acts as a proxy between the two ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
You really should rethink your design here. Having classes that are inter-dependent is not easy to write, and become impossible to maintain. There are tons of options you can take for making this code cleaner, but I will only provide one here:
class Two; class Three; class One { private: int _One; public: std::tr1::shared_ptr<Two*> _Two; std::tr1::shared_ptr<Three*> _Three; One() : _Two(new Two), _Three(new Three) { } }; class Two { private: int _Two; public: std::tr1::shared_ptr<One*> _One; std::tr1::shared_ptr<Three*> _Three; Two() : _One(new One), _Three(new Three) { } }; class Three { private: int _Three; public: std::tr1::shared_ptr<One*> _One; std::tr1::shared_ptr<Two*> _Two; Three() : _One(new One), _Two(new Two) { } };
The best solution, however, would be to redesign these classes such that they are not inter-dependent.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Hi, I'm not sure if this will work. I think that this will surely blow your memory into oblivion. When you create an One object it will create a Two and a Three object. Those will create 2 One objects a Three and a Two object. These four objects will create on there turn 3 Two, 3 Three and 2 One objects and so further and so on ... Its like you said very dangerous to desing an inter-dependen class relation !!!
codito ergo sum
-
Hi, I'm not sure if this will work. I think that this will surely blow your memory into oblivion. When you create an One object it will create a Two and a Three object. Those will create 2 One objects a Three and a Two object. These four objects will create on there turn 3 Two, 3 Three and 2 One objects and so further and so on ... Its like you said very dangerous to desing an inter-dependen class relation !!!
codito ergo sum
BadKarma wrote:
I think that this will surely blow your memory into oblivion.
He is going to have that problem anyway, it will just be on the stack instead of the heap (if he was actually able to do what he wanted to do, that is).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac