Arg! Help!

Started by
5 comments, last by OuncleJulien 19 years, 11 months ago
I have two classes (each with they''re own .h file and .cpp file. Each header file has a unique #ifndef HEADERFILENAME_H #define HEADERFILENAME_H ... class definition ... #endif ..So each header can be included multiple times throughout my program. My problem is I have two classes that reference each other and the compiler doesn''t like this. Example: class A { B b; } class B { A a; } Anyone know a way to make this ok with the compiler or is it impossible to do this? Thanks guys
Advertisement
do this:

A.h :

class B;

class A {
B b;
};

----------

B.h :

class A;

class B {
A a;
};

----------


Note that the header files are not including each other. This code is untested but it should work.
It''s impossible. Think about it. A contains a B containing an A containing a B containing an A ... it cannot be done. What the AP suggested also wouldn''t work for the reason that you cannot instantiate an object prior to definition (since its size is not known). What can be done in a manner similar to the AP''s suggestion (forward declaration) is making them contain pointers to each other (well, A could have a B that has a pointer to A, or B could have an A with a pointer to B, or they could just point to each other, but they cannot contain each other).
Go sit in the corner and think about what you just said...
It should work if you use pointers in the classes...I think.
You are correct. The only way this is doable is with pointers.
-dizzyGame Institute InternPlease rate me. :)
Sweet, thanks everyone =)

This topic is closed to new replies.

Advertisement