Overloading == operator

S

Stub

When I try to overload the == operator, it gives me an "error C2804: binary
'operator ==' has too many parameters."

bool operator==(const Store& Store1, const Store& Store2);

After Adding keyword, friend, to the above declaration, the error disappear.
Of course there're 3 parameters involved in this newly defined == operator.
I am trying to understand why the compiler complains about too many
parameters for the declaration. Is this due to that the original ==operator
takes only 2 parameters? What is this declaration conflicting with
actually?

Could someone help me to understand this? Thanks!
 
V

Victor Bazarov

Stub said:
When I try to overload the == operator, it gives me an "error C2804: binary
'operator ==' has too many parameters."

bool operator==(const Store& Store1, const Store& Store2);

After Adding keyword, friend, to the above declaration, the error disappear.
Of course there're 3 parameters involved in this newly defined ==
operator.

What do you mean "of course"? You cannot redefine the number of
the operands an operator takes. Equality (like inequality or
multiplication, or division, or comparison) operator takes _two_
and _precisely__two_ operands. A non-static member function for
an overloaded binary (two-operand) operator MUST have only one
[non-hidden] argument. A non-member function for a binary op
needs EXACTLY two operands.
I am trying to understand why the compiler complains about too many
parameters for the declaration. Is this due to that the original ==operator
takes only 2 parameters?
Yes.

What is this declaration conflicting with
actually?

Could someone help me to understand this? Thanks!

Well, I guess you just need to find a decent C++ book and read
the chapter on operator overloading. Try not to guess how
a language works. Learn instead.

Victor
 
D

David White

Stub said:
When I try to overload the == operator, it gives me an "error C2804: binary
'operator ==' has too many parameters."

bool operator==(const Store& Store1, const Store& Store2);

After Adding keyword, friend, to the above declaration, the error
disappear.

I assume this is inside a class definition. You didn't say so.

There are always 2 parameters to operator==, because it is intended for an
expression of the form op1 == op2. For a class member function, *this (the
object for which the operator is called) is implicitly the first of the
parameters, so the member function takes one parameter (for the second
operand). Example:

class A
{
int n;
public:
A(int n_) : n(n_) {}
bool operator==(const A &a) const
{
return n == a.n;
}
};

void f()
{
A a1(1), a2(2);
if(a1 == a2)
{
//...
}
}

a1 == a2 above is the same as: a1.operator==(a2).

If the function is outside the class, it will take two arguments, one for
each operand, because there is no *this outside the class. You declare it as
a friend inside the class only if you need the function to access private or
protected members. Example:

class A
{
friend bool operator==(const A &a1, const A &a2);
int n;
public:
A(int n_) : n(n_) {}
};

bool operator==(const A &a1, const A &a2)
{
return a1.n == a2.n;
}
Of course there're 3 parameters involved in this newly defined == operator.
I am trying to understand why the compiler complains about too many
parameters for the declaration. Is this due to that the original ==operator
takes only 2 parameters?

An operator== will always take 2 parameters.
What is this declaration conflicting with
actually?

No conflict. It's just wrong for an operator== to take more or less than 2
parameters (including implicit *this, where applicable).

DW
 
D

DarkSpy

When I try to overload the == operator, it gives me an "error C2804: binary
'operator ==' has too many parameters."

bool operator==(const Store& Store1, const Store& Store2);

After Adding keyword, friend, to the above declaration, the error disappear.
Of course there're 3 parameters involved in this newly defined == operator.
I am trying to understand why the compiler complains about too many
parameters for the declaration. Is this due to that the original ==operator
takes only 2 parameters? What is this declaration conflicting with
actually?

Could someone help me to understand this? Thanks!
in member function, operator== just need one parameter, because
the left value is *this, the parameter in bracket is the
right value of operator== for ex:
struct A { bool operator==(int i) {...} };
A a; if(a == 10) ...
means: a.operator==(10);

for friend keyword, this is not member functions, and no * this
in the function implicit, so need two parameter, but need a
class object or enum type for one parameter, the parameter
in bracket is:
friend bool operator==(the left value of expression, the right value
of expression)

for ex:
struct A { friend bool operator==(int i, const A &) {...} };
A a; if(10 == a) ...
means: operator==(10, a);


[comp.lang.c++]
[comp.lang.c++.moderated]
DarkSpy, A C++ Mad Dog. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top