【发布时间】:2012-03-20 07:09:00
【问题描述】:
可能重复:
Base template class data members are not visible in derived template class?
我正在尝试创建一个扩展模板化超类的模板化类。
template <class T>
class SuperClass {
public:
T my_data;
};
template <typename T2>
class SubClass : public SuperClass<T2>
{
public:
T2 f() { return my_data; }
};
int main()
{
SubClass<int> x;
return 0;
}
但是,我收到以下错误(使用 g++ 4.6.1):
test.cpp:11:18: error: ‘my_data’ was not declared in this scope
奇怪的是,如果我重新定义SubClass::f() 如下:
T2 f() { return this->my_data; }
有人明白我为什么需要this->吗?
【问题讨论】:
标签: c++ templates inheritance