C++
How can I put the definition of an inner (nested) class outside its outer (enclosing) class's definition, where the outer class has at least one instance of the inner class as a data member? I searched but the most relevant SO answer I found, Nested Class Definition in source file, does not have an example where the outer class has an inner object as a data member. I followed that answer, as far as declaring but not defining the inner class inside the outer class's definition is concerned, but my code is still broken:
struct Outer
{
struct Inner;
Inner myinner;
Outer() : myinner(2) {}
};
struct Outer::Inner
{
Inner(int n) : num(n) {}
int num;
};
int main()
{
Outer myouter;
}
It gives the error error C2079: 'Outer::myinner' uses undefined struct 'Outer::Inner'
in VC11.
And why doesn't the broken code have an effect equivalent to that of the version in which Inner
is defined within Outer
's definition, like in the following working code?
struct Outer
{
struct Inner
{
Inner(int n) : num(n) {}
int num;
} myinner;
Outer() : myinner(2) {}
};