At the point where you attempt to define S::S2
, the type S
is still an incomplete type, since its definition hasn't been completed yet. And class data members must have a complete type.
You can easily fix it like this:
struct S
{
struct S2; // declare only, don't define
// ...
};
struct S::S2
{
S s; // now "S" is a complete type
};
It is essentially a design decision of C++ to not consider a type complete until the end of its definition. This prevents many pathological situations like the following example:
struct X
{
struct Y { X a; };
int b[sizeof(Y)]; // "sizeof" requires a complete type
};