Here is the code:
template <typename T>
struct Outer {
struct InnerBase {
int n;
InnerBase() : n(42) { }
};
struct InnerChild : InnerBase {
int getN() { return n; }
};
};
And here is the problem:
In member function 'int Outer<T>::InnerChild::getN()':
error: 'n' was not declared in this scope
If the Outer is not a template, everything works fine. How so? How to fix it in the template?
InnerBaseis not a free-standing type, but actually a dependent class, since it is reallyOuter<T>::InnerBase. ThusInnerChilddoesn’t know from whom exactly it is inheriting (think of specializations*!), and thus it cannot know thatnis actually a name.To assert that
nis indeed a name of a class member, saythis->norInnerBase::n, or add a declarationusing InnerBase::n;at some point inInnerChild.*) e.g.
template <> struct Outer<int>::InnerBase { typedef int n; };