Jump to content
  • Advertisement
Sign in to follow this  
UmaN

Class dependency problem (c++)

This topic is 4216 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

Hi. I have two classes A and B whom each contain an object of the other class as a member. That is, class A has an object of class B as a member in the class declaration and vice versa for class B. I have included each header file etc. in the classes but the compiler stops and says "undefined" when I try to compile. Is there any nice way around this? I want to keep these classes' headers and their implementations in separate files.

Share this post


Link to post
Share on other sites
Advertisement
You only require the definition of a class when you define an instance, inherit from it, or access one of its members. Pointer-to-class or reference-to-class members, as well as function arguments, do not require the class definition and a forward declaration is sufficient.

So, use a forward declaration in the header files (without including the other header) and include the headers in the implementation files.

Share this post


Link to post
Share on other sites
You could prototype the class...

ClassA.h
#ifndef CLASSA_H
#define CLASSA_H

class ClassB; // Class prototype
class ClassA
{
private:
/* Your data */
ClassB InstanceOfClassB;

/* ... */
};

#endif




ClassB.h
#ifndef CLASSB_H
#define CLASSB_H

class ClassA; // Class prototype
class ClassB
{
private:
/* Your data */
ClassA InstanceOfClassA;

/* ... */
};

#endif




However, this won't work if you need to store the class in a container of some sort such as std::vector or boost::scoped_ptr, which require completely defined types. If that is the case, then someone else will need to help you with that.

EDIT - Performed a little fix. Now you just have to include the header files in the implementation like ToohrVyk said.

Share this post


Link to post
Share on other sites
Whoa - are you dealing with pointers to objects of ClassA and ClassB, or actual objects themselves? If the latter then you'll run into problems as objects of both classes would be infinitely big...

Share this post


Link to post
Share on other sites
Quote:
Original post by TheShadow344
You could prototype the class...

ClassA.h
*** Source Snippet Removed ***

ClassB.h
*** Source Snippet Removed ***

However, this won't work if you need to store the class in a container of some sort such as std::vector or boost::scoped_ptr, which require completely defined types. If that is the case, then someone else will need to help you with that.

EDIT - Performed a little fix. Now you just have to include the header files in the implementation like ToohrVyk said.


That definitely does not work; you cannot declare instances of undefined classes. Forward declarations allow you to declare pointers and references only. But I'm sure you meant to stick a * or & in there. [wink]

(Not to mention the fact that A and B containing instances of B and A respectively would be a recursive definition - you might as well say class A { A anotherA; }; [smile])

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!