[問題] 很簡單的樣板的問題

看板C_and_CPP (C/C++)作者 (生活以下)時間16年前 (2009/03/31 13:59), 編輯推噓5(506)
留言11則, 4人參與, 最新討論串1/1
#include <iostream> using namespace std; template <class T> T max(T a, T b) { return a>b? a:b; } int main() { int m=3, n=2; cout << max(m, n) << endl; system("pause"); } 下面是錯誤訊息 In function `int main()': call of overloaded `max(int&, int&)' is ambiguous candidates are: T max(T, T) [with T = int] const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int] 書本上只有說明 沒有範例 看不出來哪裡有問題 可以幫忙看一下嗎 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.42.204.109

03/31 14:07, , 1F
你的問題是啥?
03/31 14:07, 1F

03/31 14:09, , 2F
就是我這樣過不了
03/31 14:09, 2F
※ 編輯: Ygulu 來自: 114.42.204.109 (03/31 14:11)

03/31 14:15, , 3F
把max換個名字 跟標準庫內的function撞名了
03/31 14:15, 3F

03/31 14:17, , 4F
還是推崇一下VC2008EXP
03/31 14:17, 4F

03/31 14:17, , 5F
error C2668: 'max' : 模稜兩可的呼叫多載函式
03/31 14:17, 5F

03/31 14:17, , 6F
這樣的錯誤訊息就相當清楚了
03/31 14:17, 6F

03/31 14:17, , 7F
不然就不要using namespace std;
03/31 14:17, 7F

03/31 14:18, , 8F
using std::cout; using std::endl就好
03/31 14:18, 8F

03/31 14:25, , 9F
把max改掉就可以了 感謝樓上各位
03/31 14:25, 9F

03/31 19:50, , 10F
可以把func template放到head 而且不要using namespace std
03/31 19:50, 10F

03/31 19:51, , 11F
再需要的函式內using就好了
03/31 19:51, 11F
文章代碼(AID): #19qR4c5S (C_and_CPP)
文章代碼(AID): #19qR4c5S (C_and_CPP)