模板继承内部类访问问题 [英] Template inheritance inner class access problem
问题描述
我不敢相信 gcc 不会接受以下代码...请告诉我,如果从基本模板访问内部类是真的不可能,或者我丢失了一些东西? p>
I can't believe gcc won't accept the following code... Please tell me if accessing an inner class from a base template is really not possible or am i missing something?
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
是一个依赖的名称,所以你必须告诉编译器延迟名称查找,直到模板被实例化,通过限定它的基类名:
The problem is that MyStruct
is a dependent name, so you have to tell the compiler to defer name lookup until the template is instantiated by qualifying it with the base class name:
typename BaseClass<T>::MyStruct ms1;
更多信息,我将阅读Parashift C ++ FAQ条目,当我的模板派生类使用嵌套类型时,为什么我会收到错误,它继承自它的template-base-class?
For more information, I'd read the Parashift C++ FAQ entry, "Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?"
这篇关于模板继承内部类访问问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!