I have an issue with the following kind of class nesting:
#include <iostream>
class A;
class B
{
public:
void test(A* a);
{
a->x = 'a';
};
};
class A
{
public:
char x;
B b;
};
int main(void)
{
A* a = new A();
}
In detail class A has an instance of class B and class B modifies an object of class A in a method. This throws the error:
main.cpp:16:4: error: invalid use of incomplete type ‘class A’ a->x = 'a';
So far no surprise since the compiler does not know the properties of A yet. A simple workaround would be to put the definition of B::test(A* a) after the full declaration of class A, like this:
#include <iostream>
class A;
class B
{
public:
void test(A* a);
};
class A
{
public:
char x;
B b;
};
void B::test(A* a)
{
a->x = 'a';
}
int main(void)
{
A* a = new A();
}
However, in my actual code the classes are in separate header files and the definitions of the methods in respective .cpp files, which get compiled together with the header files.
What would be an appropriate workaround for this kind of problem?