Re: [問題] template+operator overloading的問題

看板C_and_CPP (C/C++)作者 (大屁孩~)時間16年前 (2009/03/19 23:06), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串2/2 (看更多)
test<float> 和 test<A>實際上是兩個不同的類(實際上你可以把template看做帶型態的 宏)。而temp1+temp2調用的是 test<float>::operator+(test<float>&) - 如你的模板 定義。 如果要實現temp1+temp2,你必須要特化template<float>然後在裡面加一個 template <float> test::operator+(test<A>&) ※ 引述《x3795566 (冀憶)》之銘言: : 假設我有一個結構為A 一個class為test : 然後在main中 我宣告了兩個物件temp1,temp2 : test<float> temp1; : test<A> temp2; : 然後我欲使temp1+temp2 所以打算多載運算子 : 而有下面程式碼(只貼定義部份) : template <class T> : float test<T>::operator+(test& i) : { : . : . : . : } : 編譯結果顯示錯誤 : no match for 'operator+' in 'temp1 + temp2' : candidates are: float test<T>::operator+(test<T>&) [with T = float] : 我的編譯器為Dev-C++ : 我有試著將test<A> temp2 改為 test<float> temp2 : 編譯就成功了 所以我想問題在於operator+(test& i)的傳入參數型態 : 不知道高手可以告知我那邊的型態要如何寫呢? 感謝<(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.82.82.186

03/20 01:23, , 1F
可是這樣會讀不到A結構說 因為我的class是寫在另外的檔案
03/20 01:23, 1F

03/20 11:45, , 2F
這篇怎麼全是大圈仔的用語啊
03/20 11:45, 2F

03/20 12:06, , 3F
可能原 PO 來自對岸吧 XD
03/20 12:06, 3F
文章代碼(AID): #19mbztk9 (C_and_CPP)
文章代碼(AID): #19mbztk9 (C_and_CPP)