【发布时间】:2011-03-17 17:00:49
【问题描述】:
可能的重复:
GCC problem : using a member of a base class that depends on a template argument
Why does GCC need extra declarations in templates when VS does not?
Why doesn’t a derived template class have access to a base template class
iphone compiler inherited templated base classes with passed through type not being expanded in time (just look)
抱歉标题混乱,我能想到的最好的。
这里有一些代码来说明我的问题...
一个基本模板类:
template<class T> class TestBase
{
public:
int someInt;
};
正在尝试用另一个模板类继承 TestBase...
这会在编译时得到“someInt was not declared in this scope”:
template<class X> class TestSub : public TestBase<X>
{
void testf()
{
someInt = 0; //Error: "someInt was not declared in this scope"
}
};
二)
这很好用(区别在于我明确指定了 TestBase 的模板输入)
template<class X> class TestSub : public TestBase<string>
{
void testf()
{
someInt = 0;
}
};
为什么来自 (A) 的 TestSub 不能像在 (B) 中那样正确继承 someInt?
提前致谢。
【问题讨论】:
-
解决这个问题有4种方法:1)使用前缀
TestBase<T>::someInt,2)使用前缀this->someInt,3) 添加语句using TestBase<T>::someInt, 4) 使用启用许可模式的全局编译器开关。这些解决方案的优缺点在stackoverflow.com/questions/50321788/… 中进行了描述
标签: c++ inheritance templates