# 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 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 on other sites
You could prototype the class...

ClassA.h
#ifndef CLASSA_H#define CLASSA_Hclass ClassB;  // Class prototypeclass ClassA{  private:    /* Your data */    ClassB InstanceOfClassB;    /* ... */};#endif

ClassB.h
#ifndef CLASSB_H#define CLASSB_Hclass ClassA;  // Class prototypeclass 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 on other sites
Thank you both. That solved it.

##### 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 on other sites
Quote:
 Original post by TheShadow344You 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])

Stop.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978471

• 10
• 12
• 22
• 13
• 33