我有一个C++模板的奇怪问题,我不明白为什么以下代码不起作用:
#include <iostream>
template <typename A, typename B>
class TypePair {
public:
typedef A First;
typedef B Second;
};
template <typename P>
class Foo {
public:
Foo(P::First f, P::Second) {
std::cout
<< "first = " << f << std::endl
<< "second = " << s << std::endl;
}
};
int main(int argc, char **argv) {
Foo<TypePair<int, double> > foo(42, 23.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该代码产生以下错误:
$ g++ templates.cpp -o templates
templates.cpp:14: error: expected ‘)’ before ‘f’
templates.cpp: In function ‘int main(int, char**)’:
templates.cpp:23: error: no matching function for call to ‘Foo<TypePair<int, double> >::Foo(int, double)’
templates.cpp:12: note: candidates are: Foo<TypePair<int, double> >::Foo()
templates.cpp:12: note: Foo<TypePair<int, double> >::Foo(const Foo<TypePair<int, double> >&)
Run Code Online (Sandbox Code Playgroud)
对我来说,代码看起来完全没问题,但是g ++显然有自己的看法^^有什么想法吗?
塞巴斯蒂安
Arm*_*yan 15
使用
Foo(typename P::First f, typename P::Second s)
Run Code Online (Sandbox Code Playgroud)
由于P是模板参数,因此P :: First和P :: Second是从属名称,因此您必须明确指定它们是类型名称,而不是静态数据成员.有关详细信息,请参见
| 归档时间: |
|
| 查看次数: |
3905 次 |
| 最近记录: |