Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded operator doesn't get detected

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

like image 431
Zeflo Avatar asked Jun 11 '11 05:06

Zeflo


People also ask

How do you declare an overloaded operator?

Overloaded operators are implemented as functions and can be member functions or global functions. An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator.

Why we Cannot overload operators?

These operators cannot be overloaded because if we overload them it will make serious programming issues. For an example the sizeof operator returns the size of the object or datatype as an operand. This is evaluated by the compiler. It cannot be evaluated during runtime.

Why is there no operator overloading in Java?

Java doesn't allow user defined operator overloading because if you allow programmer to do operator overloading they will come up with multiple meanings for same operator which will make the learning curve of any developer hard and things more confusing and messing.

Which 4 operators Cannot be overloaded?

Some operators cannot be overloaded which include sizeof operator, typeid, Scope resolution (::), Class member access operator (.), Ternary or conditional operator (?:).


1 Answers

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?

like image 84
AProgrammer Avatar answered Oct 12 '22 23:10

AProgrammer