我不能相信
gcc不会接受以下代码…请告诉我,如果从基本模板访问内部类是真的不可能或我是缺少某些东西?
template <class T> class BaseClass { public: struct MyStruct { T *data; }; }; template <class T> class ChildClass : public BaseClass <T> { public: void problem() { MyStruct ms1; //error: 'MyStruct' was not declared in this scope ::MyStruct ms2; //error: '::MyStruct' has not been declared BaseClass<T>::MyStruct ms3; //error: expected `;' before 'ms3' } };
解决方法
问题是MyStruct是一个依赖的名称,所以你必须告诉编译器推迟名字查找,直到通过使用基类名称来对模板进行实例化:
typename BaseClass<T>::MyStruct ms1;
有关更多信息,我将阅读Parashift C FAQ条目“Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?”