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

作者: john2223k (配槍)   2020-06-07 13:18:24
開發平台(Platform): (Ex: Win10, Linux, ...)
macOS中安裝Vs Code
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
gcc編譯
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
想請問各位大大~小弟新手,
練習時利用重載operator==運算符,想要實現在List中利用List的.remove()傳入自
定義數據類型,但卻報此錯誤:
錯誤結果(Wrong Output):
[email protected] 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的版本,報的錯誤是相同的。
作者: Lipraxde (Lipraxde)   2020-06-07 13:52:00
看起來沒什麼問題 https://ideone.com/2GA6cj錯誤訊息應該不止這麼一點,底下應該還有。另外,通常是不會在 header 前面使用 using namespace對了,也許宣告成 const member function 會可以
作者: Jockey66666 (往事已成追憶)   2020-06-07 14:00:00
解1 : 改成non-member function的版本解2 : 改成 const member function其實錯誤訊息講得蠻清楚的了.
作者: Lipraxde (Lipraxde)   2020-06-07 15:15:00
跟 template 有關錯誤訊息常常會噴一大串,熟悉的話可能看一點就知道原因了,不過剩下的那些訊息還是很有用
作者: kingofsdtw (不能閒下來!!)   2020-06-08 01:05:00
別直接return吧? 抓bug很難切

Links booklink

Contact Us: admin [ a t ] ucptt.com