未检测到重载运算符

发布于 2024-11-15 02:36:58 字数 759 浏览 4 评论 0原文

首先介绍一点背景知识,我使用模板制作了一个双向链表。我有一个“帐户”类,其中重载了“==”运算符来比较帐户 ID。我创建了一个链接列表来保存帐户。

当我将新帐户添加到列表中时,它会调用“包含?”调用 == 运算符的方法。这是调用错误的地方,g++ 告诉我

sll.h:在成员函数“bool list::contains(T) [with T = account]”中:

customer.h:25:35:从此处实例化

sll.h:261:3:错误:“temp->node::data == item”中的“operator==”不匹配

account.h:36:6:注意:候选者是:bool account::operator==(account&)

我一直在调查已经几个小时了,我却无法弄清楚到底是怎么回事。我怀疑这可能与我正在使用模板有关。我创建了测试程序来查看我是否正确重载了运算符并且它按预期工作。

另请注意:有一个客户类,它是不幸的是,我不能发布超过 2 个超链接,所以相信这个类是正确制作的 =P

因为代码有点长,所以我使用了 Pastie。 :

<一href="http://pastie.org/2050806" rel="nofollow noreferrer" title="链表类">链表类

账户类别

A little back ground first, I've made a doubly linked list using templates. I have an "account" class in which I've overloaded the "==" operator to compare the account ID's. I've created a linked list to hold the accounts.

When I add a new account to the list, it calls the "contains?" method which calls the == operator. This is where the error is invoked and g++ tells me

sll.h: In member function ‘bool list::contains(T) [with T = account]’:

customer.h:25:35: instantiated from here

sll.h:261:3: error: no match for ‘operator==’ in ‘temp->node::data == item’

account.h:36:6: note: candidate is: bool account::operator==(account&)

I've been investigating for a few hours now and I can't get to the bottom of it. I suspect it may have something to do with the fact that I"m using templates. I created test program to see if I'm overloading the operator correctly and it works as expected.

Also note: there is a customer class which is what the accounts list in contained in and this is what's called "add" method. Unfortunately I can't post more than 2 hyperlinks so just take my word for it that this class is properly made. =P

As the code is somewhat long I used pastie:

Linked list class

Account class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-11-22 02:36:58

当您尝试与 const 参数进行比较时,您的运算符 == 将其正确的参数作为非 const 引用。顺便说一句,您的运算符 == 也是非常量成员。

  1. 你的operator==应该是一个const成员并接受一个const引用参数

  2. 你的operator==最好是一个自由函数或两个参数不会以相同的方式处理转换,并且您有从字符串到帐户的隐式转换。

  3. 真的需要隐式转换吗?

  4. 您有公共数据成员帐户。真的想要吗?

Your operator== takes its right argument as non const reference while you are trying to compare with a const parameter. BTW, your operator== is also a non const member.

  1. Your operator== should be a const member and take a const reference paramter

  2. Your operator== would be better to be a free function or the two parameters won't be handled in the same way for conversion and you have an implicit conversion from string to account.

  3. Is that implicit conversion really wanted?

  4. You have public data member is account. Is it really wanted?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文