I have a problem with creating some form of hierarchy with different types of objects. I have a class that has a member of another class, for example:
class A
{
public:
A(){}
~A(){}
void addB(B* dep){
child = dep;
dep->addOwner(this);
}
void updateChild(){
child->printOwner();
}
void print(){
printf("Printing...");
}
private:
B* child;
};
And this is class B:
class B
{
public:
void addOwner(A* owner){
ownerObject = owner;
}
void printOwner(){
ownerObject->print();
}
private:
A* ownerObject;
};
Calling function "B" from class "A" works just fine, but when you try it on the contrary, a compiler error occurs because A is not defined in B. Actually this happens with the include and forward declarations, but I guess its cross-reference problem that the compiler can't decide.
Is there a chance to solve this problem or should I rethink my design?
source
share