   c++ Forward Class Declerations
  How do you code the following. If i have 2 classes in a .h file and class A uses class B, but class B is declared after class A (show below)class A {    B b;}class B {}I thought that all i had to do was put a forward decleration of class B before class A, but this doesnt work (gives error error C2079: 'A::b' uses undefined class 'B'). Im using VC++ Express...How do you do this?Thanks   
  Since you've got the two classes each circularly owning an instance of the other, you've managed to create kind of an infinite loop of storage.You probably want one or both of these to have a pointer to the other class and not an instance.  Why?  Because if both have an instance of each other you have storage like this:A instance;instance.b.a.b.a.b.a.b.a.b.a.b.a.b.a.b.a.b.a // goes on forever;Quote:Original post by legalizeSince you've got the two classes each circularly owning an instance of the other, you've managed to create kind of an infinite loop of storage.Sorry, my mistake, class B isnt supposed to have an instance of class A in it. Thanks for the answer tho, ive never thought about that before :)   ;  Yes, following on from what legalize said, you would have to make access by pointer before you can use a forward declaration.  Something like:#include <memory>class B;class A{    std::auto_ptr<B>pB;};class B{    std::auto_ptr<A>pA;};Edit : In your revised case you can either make sure that the definition  of B comes first or use a pointer and declaration.--random   ;Quote:Original post by deviantDarrenQuote:Original post by legalizeSince you've got the two classes each circularly owning an instance of the other, you've managed to create kind of an infinite loop of storage.Sorry, my mistake, class B isnt supposed to have an instance of class A in it. Thanks for the answer tho, ive never thought about that before :)In the revised case, the answer is simple then: put B before A in the file.   ;Quote:I thought that all i had to do was put a forward decleration of class B before class A, but this doesnt work (gives error error C2079: 'A::b' uses undefined class 'B').Remember, in C++ a translation unit is compiled top-down, and in isolation. The compiler does not remember anything about prior translation units, and it does not know how to jump ahead in the source file to look for defintions. Thus, when a definition is required at a given point, that definition must exist above that point in the file.When defining a class, the compiler must be able to determine the size of the class (among other things). This means that it must have the definitions of all sub-objects and base classes. A forward declaration is just that -- a declaration -- it provides no information about the size or makeup of the class, just an assertion that "this class exists...somewhere." The class type is incomplete after a declaration, and its size cannot be determined, so you can't put a instance of that type inside another type or it wouldn't be possible to determine the size of the enclosing type, either.However, the size of a pointer or reference to any type T is fixed, and doesn't involve the size of T itself. This is why you get the error you get, whereas other posters have observed that a pointer or reference (or placing the defintion of B above A) will solve the problem.   ;Quote:Original post by jpetrieQuote:I thought that all i had to do was put a forward decleration of class B before class A, but this doesnt work (gives error error C2079: 'A::b' uses undefined class 'B').Remember, in C++ a translation unit is compiled top-down, and in isolation. The compiler does not remember anything about prior translation units, and it does not know how to jump ahead in the source file to look for defintions. Thus, when a definition is required at a given point, that definition must exist above that point in the file.When defining a class, the compiler must be able to determine the size of the class (among other things). This means that it must have the definitions of all sub-objects and base classes. A forward declaration is just that -- a declaration -- it provides no information about the size or makeup of the class, just an assertion that "this class exists...somewhere." The class type is incomplete after a declaration, and its size cannot be determined, so you can't put a instance of that type inside another type or it wouldn't be possible to determine the size of the enclosing type, either.However, the size of a pointer or reference to any type T is fixed, and doesn't involve the size of T itself. This is why you get the error you get, whereas other posters have observed that a pointer or reference (or placing the defintion of B above A) will solve the problem.Excellent answer and very easy to understand, Thanks :)Ive changed the member in class A to a pointer now as mentioned by other posters. Thanks for the help   