【发布时间】:2010-04-13 10:09:55
【问题描述】:
template<class T>
class test
{
public:
test()
{
}
test(T& e)
{
}
};
int main()
{
test<double> d(4.3);
return 0;
}
使用 g++ 4.4.1 编译,出现以下错误:
g++ test.cpp -Wall -o test.exe
test.cpp: In function 'int main()':
test.cpp:18: error: no matching function for call to 'test<double>::test(double)
'
test.cpp:9: note: candidates are: test<T>::test(T&) [with T = double]
test.cpp:5: note: test<T>::test() [with T = double]
test.cpp:3: note: test<double>::test(const test<double>&)
make: *** [test.exe] Error 1
但是,这是可行的:
double a=1.1;
test<double> d(a);
为什么会这样? g++ 是否有可能无法将文字表达式 1.1 隐式转换为双精度? 谢谢。
【问题讨论】:
-
你的构造函数不需要双精度,它需要一个双精度引用。
标签: c++ templates constructor