Reputation: 57
I'm trying to overload the == operator in a project I'm doing. the declaration and definitions are:
friend bool operator==(const TradeItem& item);
bool TradeItem::operator==(const TradeItem& item)
when i compile this it says: 'bool operator==(const TradeItem&)' must take exactly two arguments. So i rework it to have two arguments like this:
friend bool operator==(const TradeItem& i1, TradeItem& i2);
bool TradeItem::operator==(const TradeItem& i1, TradeItem& i2)
but when i compile this it tells me it takes exactly one argument.... talk about giving me the runaround. anyone know whats going wrong?
Upvotes: 1
Views: 6405
Reputation: 83527
Remember that ==
is a binary operator. This means it must always have two arguments. If you overload operator==()
as a member function, one of those arguments is the implicit this
which is passed to every member function.
In your code, you declare a global operator==()
function that is a friend
of the TradeItem
class. At the same time, you are define a operator==()
as a member of TradeItem
. Pick one; you should not do both.
Upvotes: 3
Reputation: 88155
The problem is that:
friend bool operator==(const TradeItem& item);
requires two arguments and:
bool TradeItem::operator==(const TradeItem& item)
requires one argument.
This is because the non-member version of operator==
always requires two and the member version requires one. Your friend declaration declares a non-member operator overload.
class C {
friend bool operator==(const C &lhs, const C &rhs);
public:
bool operator==(const C& rhs);
};
bool operator==(const C &lhs, const C &rhs) { return true; }
bool C::operator==(const C& rhs) { return true; }
Also you only need to use the two member version if you need to permit type conversions on the left hand side. E.g.:
bool operator==(int lhs, const C &rhs);
bool operator==(double lhs, const C &rhs);
10 == C();
3.0 == C();
And you may be able to get away without declaring these overloads as friends of C. Maybe something like:
class C {
public:
bool operator==(const C& rhs);
bool operator==(int rhs);
bool operator==(double rhs);
};
bool operator==(int lhs, const C &rhs) { return rhs == lhs; }
bool operator==(double lhs, const C &rhs) { return rhs == lhs; }
bool C::operator==(const C& rhs) { return true; }
bool C::operator==(int rhs) { return *this == convert_int_to_C(rhs); }
bool C::operator==(double rhs) { return *this == convert_double_to_C(rhs); }
Also since checking for equality shouldn't change the object you should const
qualify the member functions:
class C {
bool operator== (C const &rhs) const;
}
Upvotes: 6
Reputation: 158469
The class member version of operator ==
takes one argument and the friend version takes two arguments so it should be something like this:
friend bool operator==(const TradeItem& i1, TradeItem& i2);
bool TradeItem::operator==(const TradeItem& i )
This is because the member version has a hidden this
argument. The member version will also have precedence over the friend version. You should choose one or the other.
Upvotes: 0
Reputation: 1319
The operator== overload that takes in two arguments must not be a member function - declare it as a bool operator==(const T&, const T&)
.
Upvotes: 0