Skip to main content
gamedev.net
🔒 Locked

Arg! Help!

Started by OuncleJulien May 8, 2004 at 1:43 PM 5 replies 400+ views
Original Post
OuncleJulien
OuncleJulien
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
Miserable
Miserable
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).
yzzid
yzzid
You are correct. The only way this is doable is with pointers.
-dizzyGame Institute InternPlease rate me. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.