I am writing a small c++ - program containing a similar structure to the following:
class A {
B * someObjects;
};
typedef A* APointer;
struct B{
APointer a;
int n;
}
Trying to compile this gives a "identifier is undefined" error since struct B is not known inside class A. Otherwise declaring struct B before class A should still give a similar error, since then B does not know APointer, or APointer does not know A. Is there any possibility to make class A and struct B being good friends? Thanks in advance!
You need to forward declare
B
as the compiler has no idea whatB
is when it is used inA
.B
is considered an incomplete type inA
and you are allowed to have a pointer or reference toB
inA
. You can change your code to: