[問題] 新手Overloading operator ==問題請益

看板C_and_CPP作者 (配槍)時間4年前 (2020/06/07 13:18), 4年前編輯推噓1(109)
留言10則, 3人參與, 4年前最新討論串1/1
開發平台(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

06/07 13:52, 4年前 , 1F
看起來沒什麼問題 https://ideone.com/2GA6cj
06/07 13:52, 1F

06/07 13:52, 4年前 , 2F
錯誤訊息應該不止這麼一點,底下應該還有。另外,通常
06/07 13:52, 2F

06/07 13:52, 4年前 , 3F
是不會在 header 前面使用 using namespace
06/07 13:52, 3F

06/07 13:56, 4年前 , 4F
對了,也許宣告成 const member function 會可以
06/07 13:56, 4F
謝謝回覆!完全可以了,Header部分也非常感謝!我會注意這個習慣。

06/07 14:00, 4年前 , 5F
解1 : 改成non-member function的版本
06/07 14:00, 5F

06/07 14:01, 4年前 , 6F
解2 : 改成 const member function
06/07 14:01, 6F

06/07 14:01, 4年前 , 7F
其實錯誤訊息講得蠻清楚的了.
06/07 14:01, 7F
感謝回覆!我可能新手還比較看不懂錯誤訊息,已經利用const member function 解決 ※ 編輯: john2223k (39.8.37.17 臺灣), 06/07/2020 14:51:00

06/07 15:15, 4年前 , 8F
跟 template 有關錯誤訊息常常會噴一大串,熟悉的話可
06/07 15:15, 8F

06/07 15:15, 4年前 , 9F
能看一點就知道原因了,不過剩下的那些訊息還是很有用
06/07 15:15, 9F
感謝,確實是噴出一大串,有寫第幾行錯誤的訊息,不過因為實在太長就沒有貼出~會練 習閱讀這些訊息 ※ 編輯: john2223k (39.8.37.17 臺灣), 06/07/2020 15:18:43

06/08 01:05, 4年前 , 10F
別直接return吧? 抓bug很難切
06/08 01:05, 10F
文章代碼(AID): #1Ut7YZHU (C_and_CPP)