作者john2223k (配槍)
看板C_and_CPP
標題[問題] 新手Overloading operator ==問題請益
時間Sun Jun 7 13:18:24 2020
開發平台(Platform): (Ex: Win10, Linux, ...)
macOS中安裝Vs Code
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
gcc編譯
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
想請問各位大大~小弟新手,
練習時利用重載operator==運算符,想要實現在List中利用List的.remove()傳入自
定義數據類型,但卻報此錯誤:
錯誤結果(Wrong Output):
johnsu@MacBook-Pro list % g++ list.cpp -o main.out
In file included from list.cpp:4:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/list:2150:18:
error:
invalid operands to binary expression ('const
std::__1::__list_const_iterator<Hero, void
*>::value_type' (aka 'const Hero') and 'const
std::__1::list<Hero, std::__1::allocator<Hero>
>::value_type' (aka 'const Hero'))
if (*__i == __x)
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
http://codepad.org/7tWj0gSC
補充說明(Supplement):
我嘗試過再重載參數裡面傳入const Hero& h跟沒有const的版本,報的錯誤是相同的。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.71.198.39 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1591507107.A.45E.html
→ Lipraxde: 錯誤訊息應該不止這麼一點,底下應該還有。另外,通常 06/07 13:52
→ Lipraxde: 是不會在 header 前面使用 using namespace06/07 13:52
→ Lipraxde: 對了,也許宣告成 const member function 會可以06/07 13:56
謝謝回覆!完全可以了,Header部分也非常感謝!我會注意這個習慣。
→ Jockey66666: 解1 : 改成non-member function的版本06/07 14:00
→ Jockey66666: 解2 : 改成 const member function06/07 14:01
→ Jockey66666: 其實錯誤訊息講得蠻清楚的了.06/07 14:01
感謝回覆!我可能新手還比較看不懂錯誤訊息,已經利用const member function 解決
※ 編輯: john2223k (39.8.37.17 臺灣), 06/07/2020 14:51:00
→ Lipraxde: 跟 template 有關錯誤訊息常常會噴一大串,熟悉的話可06/07 15:15
→ Lipraxde: 能看一點就知道原因了,不過剩下的那些訊息還是很有用06/07 15:15
感謝,確實是噴出一大串,有寫第幾行錯誤的訊息,不過因為實在太長就沒有貼出~會練
習閱讀這些訊息
※ 編輯: john2223k (39.8.37.17 臺灣), 06/07/2020 15:18:43
推 kingofsdtw: 別直接return吧? 抓bug很難切 06/08 01:05