0
class B;

class A
{
    B b;
    A();

};

class B
{
    A a;
    B();
};

I have two classes as follows. For some reason, even though I forward declare class B, I have an error that says that:

field 'b' has an incomplete type! 

Why is this the case?

Raaj
  • 363
  • 1
  • 3
  • 12
  • Should you use pointer/reference for at least one member ? (currently, your classes would have infinite size). – Jarod42 Mar 10 '16 at 09:08
  • asking why in this particular case forward declaration doesn't work isn't the same as asking `Can you explain "forward declaration" further?` or `When can I use a forward declaration?`, you could have marked as duplicate to a real duplicate like this one `http://stackoverflow.com/questions/20202231/slaying-the-circular-dependence`, which actually contains a technical answer (why can't it work ? : answer 1) and a conceptual answer (why this should never be allowed to work ? answer 2). If you're going to block any answer by marking as duplicate, please take time to link a REAL duplicate – Guiroux Mar 10 '16 at 09:37

1 Answers1

0

When you declare an instance of a class, you need the class to be fully defined since the compiler needs to know its size.

If you only declare a pointer or a reference then the compiler only needs the symbol name.

So if you change A::b to a pointer (i.e. B* b) or reference (B& b) then it will work.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621