I was trying to use nested classes inside a template class. See the code snippet below:
template <class T>
class OutterTemplate {
public:
class InnerBase {
protected:
const char* name_;
public:
virtual void print() {
cout << name_ << endl;
}
void setName(const char* n) {
name_ = n;
}
};
private:
class Inner : public InnerBase {
public:
virtual void print() {
cout << name_;
cout << " and ";
InnerBase::print();
}
};
public:
static InnerBase* getInner() {
return new Inner();
}
};
int main() {
auto q = OutterTemplate<int>::getInner();
q->setName("Not working");
q->print();
}
I got error "error: 'name_' was not declared in this scope" when trying to compile this code. I have check if "outter" is not a template class, there is no such problem. Can anyone explain why this error with template classes and how to enable access to members of based class in case of nested classes inside template class?