PTT
Submit
Submit
选择语言
正體中文
简体中文
PTT
C_and_CPP
[问题] 新手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很难切
继续阅读
[问题] multi-thread计时的问题
icetofux
[问题] Linked List的delete用法
Ruination
[问题] 想要有全域变量效果但是能控制lifetime
ofd168
[问题] 新增object时候,自动新增执行
ofd168
[问题] C++ SLT map<struct*,int> 的行为??
s89162504
[问题] IEEE 754 浮点数运算以及rounding请益
ayn775437403
[问题] 做到读取和绘制shapefile或tiff档的视窗
e25820
[闲聊] CompilerDev编译器开发 新版连署
mshockwave
[问题] SerialPort读取长度
getaway
[问题] main()和main(void)的差别
pandaren0905
Links
booklink
Contact Us: admin [ a t ] ucptt.com