call of overloaded ... is ambiguous

照着c++ primer上寫的一個函數模板例子

using namespace std;

template <class T>
T min(T a, T b) {
return a < b ? a : b;
}

int main() {
cout<<min(10, 32)<<endl;
cout<<min(24.1, 2.0)<<endl;
return 0;
}



出現瞭如下錯誤:

function_template.cpp: In function `int main()':
function_template.cpp:11: error: call of overloaded `min(int, int)' is

ambiguous
function_template.cpp:6: note: candidates are: T min(T, T) [with T = int]
D:/Program Files/mingw/MinGW/bin/../lib/gcc/
mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algobase.h:151:
note: const _Tp& std::min(const _Tp&, const _Tp&)
[with _Tp = std::streamsize]

function_template.cpp:12: error: call of overloaded `min(double, double)'

is ambiguous
function_template.cpp:6: note: candidates are: T min(T, T) [with T =

double]
D:/Program Files/mingw/MinGW/bin/../lib/gcc/
mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algobase.h:151:
note: const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = double]


原因是std中已經有了一個名字叫min的函數(在stl_algobase.h文件的148行),導致名字衝突,
發佈了45 篇原創文章 · 獲贊 0 · 訪問量 2508
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章