I want to create two classes using each other. This is the code:
class B;
class A {
public:
A(B* b) : b(b) {}
void Foo() {
b->data++;
}
B* b;
};
class B {
public:
void Boo() {
A a(this);
a.Foo();
}
int data = 0;
};
int main()
{
B b;
b.Boo();
}
When I compile it in visual Studio 2015, I got the error:
error C2027: use of undefined type 'B'
which points to line six: b->data++;
I have read the problem here, but I have used forward declarations so A should know the existence of B. Also, I don't "have two classes directly contain objects of the other type" because only B contains A but A does not contain B; it uses a pointer to B. Also, my code doesn't seem essentially different from the answer here since there the World class contains objects of Agent and the two classes use each other. But my code just can't compile. So I think my issue is not duplicate. Could you please help me work around this mutual-use issue? Thanks a lot.