forward deklaration
-
hey guys, I've got a little problem... I have two classes. One is the member of the other. but the other still needs a pointer to the first.
// A.h #include "B.h" class A; class A { public: B m_BClass; }; // B.h #include "A.h" class B; class B { public: A* m_pAClass; // <-- error here };
somehow this doesn't work... He gives me: "Error 4 Error C2143: Syntaxerror: missing ';' before '*'" cause he doesn't know the class A. Though I forward declarated it. A friend of mine told me about an "inclusion deadlock". but I cannot figure out how to solve it. i need to to a global class with all necessary includes. any hints or links? thx in advance PS: should I put the forward deklaration before the "#include" ??? -
hey guys, I've got a little problem... I have two classes. One is the member of the other. but the other still needs a pointer to the first.
// A.h #include "B.h" class A; class A { public: B m_BClass; }; // B.h #include "A.h" class B; class B { public: A* m_pAClass; // <-- error here };
somehow this doesn't work... He gives me: "Error 4 Error C2143: Syntaxerror: missing ';' before '*'" cause he doesn't know the class A. Though I forward declarated it. A friend of mine told me about an "inclusion deadlock". but I cannot figure out how to solve it. i need to to a global class with all necessary includes. any hints or links? thx in advance PS: should I put the forward deklaration before the "#include" ???your problem is you got to forward declare class A is the class B header...this is the only way class B will know about class A, but when using a pointer in class B for class A, you do not have to forward declare, because you are already including the header files for class A inside class B!!! try taking about the forward declare in both file, I both your files should compile... note in class A you have an auto member to class B object, in this case a forward declare would not be sufficient and you would need to include the header file of class B in the header file of class A. most of you include should be done in your source file(.cpp) not your header files, unless you like long builds....man there is just too much to talk about this topic and I don't want to get you lost...LOL :-D
Yours Truly, The One and Only!
-
hey guys, I've got a little problem... I have two classes. One is the member of the other. but the other still needs a pointer to the first.
// A.h #include "B.h" class A; class A { public: B m_BClass; }; // B.h #include "A.h" class B; class B { public: A* m_pAClass; // <-- error here };
somehow this doesn't work... He gives me: "Error 4 Error C2143: Syntaxerror: missing ';' before '*'" cause he doesn't know the class A. Though I forward declarated it. A friend of mine told me about an "inclusion deadlock". but I cannot figure out how to solve it. i need to to a global class with all necessary includes. any hints or links? thx in advance PS: should I put the forward deklaration before the "#include" ???Hello,
// A.h #include "B.h" class A { public: B m_BClass; void SayHelloToMyLittleFriend() {} }; // B.h class A; class B { public: A* m_pAClass; // <-- no error here anymore, A is declared (but not defined) void LetsRock(); }; // B.cpp #include "B.h" #include "A.h" void B::LetsRock() { // TODO: do something crazy with m_pAClass, like calling its member function. m_pAClass->SayHelloToMyLittleFriend(); }
Best regards, BB http://spin.bartoszbien.com
-
your problem is you got to forward declare class A is the class B header...this is the only way class B will know about class A, but when using a pointer in class B for class A, you do not have to forward declare, because you are already including the header files for class A inside class B!!! try taking about the forward declare in both file, I both your files should compile... note in class A you have an auto member to class B object, in this case a forward declare would not be sufficient and you would need to include the header file of class B in the header file of class A. most of you include should be done in your source file(.cpp) not your header files, unless you like long builds....man there is just too much to talk about this topic and I don't want to get you lost...LOL :-D
Yours Truly, The One and Only!
-
Hello,
// A.h #include "B.h" class A { public: B m_BClass; void SayHelloToMyLittleFriend() {} }; // B.h class A; class B { public: A* m_pAClass; // <-- no error here anymore, A is declared (but not defined) void LetsRock(); }; // B.cpp #include "B.h" #include "A.h" void B::LetsRock() { // TODO: do something crazy with m_pAClass, like calling its member function. m_pAClass->SayHelloToMyLittleFriend(); }
Best regards, BB http://spin.bartoszbien.com
nice work mate!
Yours Truly, The One and Only!
-
nice work mate!
Yours Truly, The One and Only!
-
what if I do not have an implementation file (cpp) because I wanna keep the code in the header? does it work, too? Or do I have to ship everything into an implementation file?
that should not matter if you put everything into a header file, but in most cases you will want to separate declaration with implementation! .... people just want to see the class members and know how to call it, they don't care how it's implemented and having to read tons of line of code figure out all your methods in a class is not the way to code. if you want to declare everything inside the head file you have to make all the method inline, otherwise you're going to get linking errors. i.e
// headfile: A.h
//
class A {
public:void MyFunc() {
//write code inline
}};
// don't do this in the headfile
void A::MyFunc() {
// write non-inline code
}I am sure you will figure it out :-D
Yours Truly, The One and Only!