[問題] template with visual studio

看板C_and_CPP作者 (fireslayer)時間15年前 (2011/01/08 03:12), 編輯推噓0(009)
留言9則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, Gcc, Linux, ...) VC++ 額外使用到的庫(Library Used) (Ex: OpenGL, ...) 問題(Question): 照著程式教學書上打的程式,不知道為何會出錯 想說應該是編譯器的種類原因 不知道要改成怎樣才可以QQ 程式碼(Code): (請善用置底文標色功能) #include"stdafx.h" #include<iostream> using namespace std; template <typename Q> Q happy (Q x, Q y,Q z) { Q max=max(x,y,z); return max; } int main() { int a,b,c; double d,e,f; char g,h,i; cout<<"Integer:"; cin>>a>>b>>c; cout<<"Double:"; cin>>d>>e>>f; cout<<"Char:"; cin>>g>>h>>i; cout<<"The max is "<<happy(a,b,c); cout<<"The max is "<<happy(d,e,f); cout<<"The max is "<<happy(g,h,i); system("PAUSE"); return 0; } 補充說明(Supplement): 先謝謝大家了(拜) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.248.77

01/08 03:13, , 1F
請貼上你的錯誤訊息 (同時也仔細的看一下)
01/08 03:13, 1F
test.cpp c:\users\michael\documents\visual studio 2010\projects\test\test\test.cpp(8): error C2064: 詞彙不等於使用 3 引數的函式 c:\users\michael\documents\visual studio 2010\projects\test\test\test.cpp(25) : 請參閱所要編譯的函式 樣板 具現化 'Q happy<int>(Q,Q,Q)' 的參考 with [ Q=int ] c:\users\michael\documents\visual studio 2010\projects\test\test\test.cpp(8): error C2064: 詞彙不等於使用 3 引數的函式 c:\users\michael\documents\visual studio 2010\projects\test\test\test.cpp(26) : 請參閱所要編譯的函式 樣板 具現化 'Q happy<double>(Q,Q,Q)' 的參考 with [ Q=double ] c:\users\michael\documents\visual studio 2010\projects\test\test\test.cpp(8): error C2064: 詞彙不等於使用 3 引數的函式 c:\users\michael\documents\visual studio 2010\projects\test\test\test.cpp(27) : 請參閱所要編譯的函式 樣板 具現化 'Q happy<char>(Q,Q,Q)' 的參考 with [ Q=char ] ※ 編輯: fireslayer 來自: 140.113.248.77 (01/08 03:16) 抱歉有點亂 還有我看不太懂甚麼是 請參閱所要編譯的函式 樣板 具現化 'Q happy<char>(Q,Q,Q)' 的參考 這不是我寫的嗎?? ※ 編輯: fireslayer 來自: 140.113.248.77 (01/08 03:18)

01/08 03:17, , 2F
你確定你有「三個參數的max」可以用嗎?
01/08 03:17, 2F

01/08 03:18, , 3F
Q max = max(x,y,z); <= 這個 max 會呼叫到什麼?
01/08 03:18, 3F
喔喔我知道了 內建的max函式只能求兩個varibles的最大值 平常用的太習慣了沒有注意到 謝謝詹姆士大XD(拜) ※ 編輯: fireslayer 來自: 140.113.248.77 (01/08 03:22)

01/08 03:20, , 4F
另一個問題,Q max = max(...) 不要讓變數跟函式同名
01/08 03:20, 4F
恩 謝謝提醒=口= ※ 編輯: fireslayer 來自: 140.113.248.77 (01/08 03:22)

01/08 03:23, , 5F
這是一種改法:http://nopaste.csie.org/64b20
01/08 03:23, 5F
萬分感謝!!!! ※ 編輯: fireslayer 來自: 140.113.248.77 (01/08 03:30)

01/08 03:50, , 6F
名稱被隱藏了啦! XD 養成加上 std:: 的習慣
01/08 03:50, 6F
請問是要加在哪邊 是每個cout.cin這些都要變成std::cout std::cin嗎? ※ 編輯: fireslayer 來自: 140.113.248.77 (01/08 14:01)

01/08 14:04, , 7F
在有可能混淆讀者/編譯器的地方加就好, 你要偏執些全
01/08 14:04, 7F

01/08 14:04, , 8F
加也不反對, using namespace 這種東西儘量不要放在函
01/08 14:04, 8F

01/08 14:05, , 9F
本體以外的地方
01/08 14:05, 9F
謝謝!! ※ 編輯: fireslayer 來自: 140.113.248.77 (01/08 22:39)
文章代碼(AID): #1D9sMWrO (C_and_CPP)