C++ requires all types to be defined before they can be used, which makes it important to include header files in the right order. Fine. But what about my situation:
Bunny.h
:
class Bunny { ... private: Reference<Bunny> parent; }
The compiler complains, because technically because I did something stupid (unrelated).Bunny
has not been completely defined at the point where I use it in its own class definition.
Apart from re-writing my template class Reference
so it takes a pointer type (in which case I can use the forward declaration of Bunny
), I don't know how to solve this.
Any suggestions?
EDIT: My Reference
class (XObject
is a base class for data mode objects):
template <class T = XObject> class Reference { public: Reference() : m_ptr (NULL) {} Reference(T* p) { m_ptr = p; if (p != NULL) ((XObject*)p)->ref(); } ~Reference() { if (m_ptr) { ((XObject*)m_ptr)->deref(); } } // ... assignment, comparison, etc. private: T* m_ptr; };
EDIT: This works fine, the problem was something else. Thanks so much for your help!