By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

Overloading == operator

P: n/a
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!


Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Stub" <st**@asof.com> wrote...
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
Jul 19 '05 #2

P: n/a
"Stub" <st**@asof.com> wrote in message
news:v4*********************@bgtnsc05-news.ops.worldnet.att.net...
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

Jul 19 '05 #3

P: n/a
在 Wed, 12 Nov 2003 02:40:27 GMT 时, "Stub" <st**@asof.com> 写了:
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. :-)
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.