下面的代码
template<typename T, typename U> class Alpha
{
public:
template<typename V> void foo() {}
};
template<typename T, typename U> class Beta
{
public:
Alpha<T, U> alpha;
void arf();
};
template<typename T, typename U> void Beta<T, U>::arf()
{
alpha.foo<int>();
}
int main()
{
Beta<int, float> beta;
beta.arf();
return 0;
}
编译失败的原因是:
../src/main.cpp:在成员函数中
‘void Beta::arf()’:
../src/main.cpp:16:错误:预期
‘int’之前的主要表达式
../src/main.cpp:16:错误:预期
‘;’位于‘int’之前
我到底该如何解决这个问题?我已经尝试了我能想到的一切。
alpha::foo
is a 附属名称 http://womble.decadent.org.uk/c++/template-faq.html#dependent, use alpha.template foo<int>()
.
假设从属名称为
- 不是类型,除非前缀为
typename
- 除非直接加上前缀,否则不是模板
template
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)