Operator == overloading given non-static member function
I have defined a class like this
using namespace std;
class foo {
public:
typedef std::pair< int, int > index;
bool operator == ( const index &l, const index &r )
{
return (l.first == r.first && l.second == r.second);
}
void bar()
{
index i1;
i1.first = 10;
i1.second = 20;
index i2;
i2.first = 10;
i2.second = 200;
if (i1 == i2)
cout << "equal\n";
}
};
However, I am getting this error on windows
error C2804: binary 'operator ==' has too many parameters
and this error on linux
operator==(const std::pair<int, int>&, const std::pair<int, int>&)β must take exactly one argument
I found this thread overload operator == complaint about "must take exactly one argument" and there seems to be a problem with static and non-static functions in the class, However, I don't know how to applythis
For example, this is not true
bool operator == ( const index &r )
{
return (this->first == r.first && this->second == r.second);
}
How can I fix this?
source to share
operator==
can be implemented in two ways:
- As a member function: in this case, the function takes one argument and is called on the left operand, which is passed as an
this
implicit pointer to the function. - As a non-member function, in this case, the function takes two arguments, the left and right operands.
Since you are implementing operator==
for std::pair
, you cannot implement it as a member function (of std::pair
). The option you're left with is a non-member function.
Inject it outside of the class like:
bool operator==(std::pair<int,int> const & l, std::pair<int,int> const & r)
{
return (l.first == r.first && l.second == r.second);
}
But then you really don't need to implement it yourself unless you want to implement it differently. The standard library has already provided a generic version operator==
for std::pair
that lexicographically compares values ββin a pair as I did above, i.e. compare first
with first
and second
with second
. If you need to compare them in different ways, only then provide your own specific definition (version without template).
The above points are worth noting how to implement operator==
when needed for certain types.
source to share
You need to move operator==
from the foo class:
bool operator == ( const foo::index &l, const foo::index &r )
{
return (l.first == r.second && l.second == r.second);
}
class foo {
public:
typedef std::pair< int, int > index;
void bar()
{
index i1;
i1.first = 10;
i1.second = 20;
index i2;
i2.first = 10;
i2.second = 200;
if (i1 == i2)
cout << "equal\n";
}
};
Also note: std::pair
already has an overload operator==
, see link, you can revise, if necessary, write your own again.
source to share
You can move this operator out of the class so you can take 2 operands. Indeed, there is no point in keeping it in the class at the moment, since you are only comparing member variables, not the class itself.
In fact, I wouldn't be surprised if it pair
already defines the operator that you write.
Edit: Yup It looks like it pair
already implements this
Two paired objects compare equal if the first elements in both objects compare to each other, and both second elements compare to each other - they must all be the same.
ps I think you meant
return (l.first == r.first && l.second == r.second);
^^^^^^
source to share