[問題] template+operator overloading的問題

看板C_and_CPP (C/C++)作者 (冀憶)時間16年前 (2009/03/19 22:55), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/2 (看更多)
假設我有一個結構為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: 140.117.194.90

03/19 23:04, , 1F
float template<class T,class T1> test<T>::operator+
03/19 23:04, 1F

03/19 23:04, , 2F
(test<T1>& t){ ....................}
03/19 23:04, 2F

03/20 00:06, , 3F
不是很懂樓上打的說~"~
03/20 00:06, 3F
文章代碼(AID): #19mbp7K0 (C_and_CPP)
文章代碼(AID): #19mbp7K0 (C_and_CPP)