Lets suppose we have a class A, which will include another class B as an element;
//ClasA.h
#ifndef _A_HEADER
#define _A_HEADER
#include "ClassB.h"
class B;
class A
{
private:
B b;
public:
A();
};
#endif
and
//ClassA.cpp
#include "ClassA.h"
A::A(){}
Now, we want class B to have a pointer to class A. So we define class B as:
//ClassB.h
#ifndef _B_HEADER
#define _B_HEADER
#include "ClassA.h"
class A;
class B
{
private:
A *a;
public:
B();
};
#endif
and
//ClassB.cpp
#include "ClassB.h"
B::B(){}
This way, we would have a class A, which would contain an object B. And The object B would have a pointer back to class A . Unfortunately, this do not work. I get the following error:
-ClassA.h:9:7: error: field ‘b’ has incomplete type. B b;
How can i fix this? And, considering that i fix it, is it worth it to implement a system like this? I mean, it surely doesn't feel very elegant.
-