template<class T>
class test
{
public:
test()
{
}
test(T& e)
{
}
};
int main()
{
test<double> d(4.3);
return 0;
}
使用G ++ 4.4.1以下錯誤編譯:C++模板:隱式轉換,沒有匹配的函數調用構造函數
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轉換爲double? 謝謝。
您的構造函數不需要double,它需要對double的引用。 – 2010-04-13 10:16:02