//Class A
#include "ClassB"
class A{
A(B)
}
//Class B
#include "ClassA"
B()...
private:
A;
Is there any way to make this work?
Using 2 Classes that include each other
Basically here's what I have:
you need a class declaration.
note the "class ClassA". this tells the compiler "there is a class called ClassA out there, and you will know about him at compile time".
//Class A#include "ClassB"class A{A(B)}//Class Bclass ClassA;B()...private:A;
note the "class ClassA". this tells the compiler "there is a class called ClassA out there, and you will know about him at compile time".
because when you do forward declaration you can only create the object dinamically.
I mean:
You need to make myA a pointer:
I mean:
class a;class b{ a myA; // <---wont work};
You need to make myA a pointer:
class a;class b{ a* myA; // <---WILL work b() //constructor { a = new a(); }};
Quote:Original post by Sfpiano
Then it tells me I'm using an undefined class.
Because a class declaration only allows you to create pointers, references to that class, or to declare functions that return or take it as parameter.
Code that actually creates variable of the class type require a full definition, as there is no other way for the compiler to know the actual size of the variable.
In your example, since A only uses B as a function parameter, it can make do with a declaration for B. However, B has a member of type A (not A* nor A&) and thus needs the full definition for A.
then add a constructor B::B(){} to your class.
and make it public! I wonder why it doesn't use the default one.
ah, don't forget to do #include "B.h" in A.cpp
and make it public! I wonder why it doesn't use the default one.
ah, don't forget to do #include "B.h" in A.cpp
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement