Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

OuncleJulien

Arg! Help!

This topic is 5132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Go sit in the corner and think about what you just said...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It should work if you use pointers in the classes...I think.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!