Class dependency problem (c++)

Started by
5 comments, last by Zahlman 17 years, 1 month ago
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.
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.
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.
Thank you both. That solved it.
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...

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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])
Stop.

Read this.

This topic is closed to new replies.

Advertisement