473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Overloading == operator

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
3 20943
"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
"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
在 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mohammad | last post by:
I'm implementing a c++ template class CScan to minipulate a series of numbers. I have implemented operator() to select a range of numbers it works fine for an expression like: scan1 = scan2(0,...
9
by: Silver | last post by:
Hi everyone, I want to overload the ! operator, so that when I write this in main x! I can get the factorial of x. The problem is, that the operator I want to overload takes no parameter. Most...
6
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
2
by: nayannovellus | last post by:
As we know that both copy constructors and overloading = opeartor copies one object to another then whats the difference between copy constructor and overloading = operator. There must be some...
3
by: Aff | last post by:
hi, i wold like to ask, why is it when overloading operator we send int value is there any way around?. e.g. class abc { protected: int *a; private: int operator(int );
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
1
by: haderika | last post by:
Hey, I'm having trouble overloading the ~ operator (the determinant of the matrix) in a case of 2x2 matrices. So i have a matrix class, with the constructor, overloading +, += and ~ operators. the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.