我對下面的代碼有疑問。當我編譯它時抱怨下面有這個錯誤。現在我的問題是,如果我刪除了「常量」,在聲明中,更改爲只:使用模板時C++「const」神祕重載函數調用
template<class T> T max(T& left, T& right);
變得好和編譯/正確執行。編譯器如何認爲該調用是模糊的?前向聲明不應該遵循實現嗎?
==== start of error message====
max.cpp:10:34: error: call of overloaded ‘max(int, int)’ is ambiguous
max.cpp:10:34: note: candidates are:
max.cpp:5:21: note: T max(const T&, const T&) [with T = int]
/usr/include/c++/4.6/bits/stl_algobase.h:210:5: note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
max.cpp:11:44: error: call of overloaded ‘max(double, double)’ is ambiguous
max.cpp:11:44: note: candidates are:
max.cpp:5:21: note: T max(const T&, const T&) [with T = double]
=======end of error message===
=============代碼從這裏開始======
#include<iostream>
using namespace std;
template<class T> T max(const T& left, const T& right);
int main(void)
{
cout<<"max int:"<< max(1,4)<<endl;
cout<<"max double:"<< max(5.02,1.002)<<endl;
}
template<class T>
T max(const T& left, const T& right)
{
return left > right? left:right;
}
'using namespace std;'的另一種情況...... –