下面这段代码有什么问题?
template<typename X>
struct A {
template<int N>
int foo() const {
return N;
}
};
template<typename X>
struct B {
int bar(const A<X>& v) {
return v.foo<13>();
}
};
#include <iostream>
using std::cout;
using std::endl;
int main() {
A<double> a;
B<double> b;
cout << b.bar(a) << endl;
return 0;
}
函数内部B::bar
编译器抱怨:
错误:类型的操作数无效
‘’和‘int’到二进制‘operator
如果 A 不是模板,则一切都可以正常编译。
Change return v.foo<13>();
to return v.template foo<13>();
因为foo
是一个从属名称,您需要明确使用.template
构造。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)