Sign in to follow this  
UmaN

Class dependency problem (c++)

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this