Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

operator overload

0 views
Skip to first unread message

Rappy

unread,
Mar 27, 2004, 5:40:03 AM3/27/04
to
having probs with addition operator overloading, error message tells me that
the binary 'operator +' has two many parameters. how can this be? it is
supposed to add two class operands.

declaration in class:
const SalesRegister operator +(const SalesRegister& lhs, const
SalesRegister& rhs);

definition in implementation file:
const SalesRegister SalesRegister::operator+(const SalesRegister
&param)
{
.....
.....
}

thank you in advance
rappy

John Harrison

unread,
Mar 27, 2004, 5:52:12 AM3/27/04
to

"Rappy" <rap...@iprimus.com.au> wrote in message
news:40655...@news.iprimus.com.au...

> having probs with addition operator overloading, error message tells me
that
> the binary 'operator +' has two many parameters. how can this be?

You have given it three operands.

> it is
> supposed to add two class operands.
>

Right.

> declaration in class:
> const SalesRegister operator +(const SalesRegister& lhs, const
> SalesRegister& rhs);
>

What you are forgetting is that all class methods have an additional
implicit parameter, the object itself (aka this).

You can define a binary operator inside the class like this

class X
{
X operator+(const X& rhs) const;
};

X X::operator+(const X& rhs) const
{
...
}

Here the implicit parameter is the left hand side operand.

Or you can declare it outside the class like this

class X
{
friend X operator+(const X& lhs, const X& rhs);
};

X operator+(const X& lhs, const X& rhs)
{
...
}

(friendship is optional but usually used).

The outside the class version is usually preferred for symmetrical operators
like +, otherwise you get different argument conversion rules applied to the
left and right hand sides of your operator, which is not a good thing.

john


Rappy

unread,
Mar 27, 2004, 7:52:53 AM3/27/04
to
this is what i meant to post:
I want to overload the addition operator, so that it may add two operands.

I have the following declaration in header file class declaration:

const SalesRegister operator +(const SalesRegister&
lhs, const SalesRegister& rhs);

and the following definition in the implementation file:

const SalesRegister operator +(const SalesRegister& lhs, const
SalesRegister& rhs)

{

SalesRegister temp;

temp.totalCostPrice = lhs.GetTotalCostPrice +
rhs.GetTotalCostPrice;

temp.totalSellPrice = lhs.GetTotalSellPrice +
rhs.GetTotalSellPrice;

return temp;

}

and I get this error message: error C2804: binary 'operator +' has too many
parameters, how is this possible

as a binary operator has two operands?

please help me, am I possibly missing a declaration somewhere?

rappy


"Rappy" <rap...@iprimus.com.au> wrote in message
news:40655...@news.iprimus.com.au...

John Harrison

unread,
Mar 27, 2004, 7:56:27 AM3/27/04
to

"Rappy" <rap...@iprimus.com.au> wrote in message
news:40657...@news.iprimus.com.au...

> this is what i meant to post:
> I want to overload the addition operator, so that it may add two operands.
>
> I have the following declaration in header file class declaration:
>
> const SalesRegister operator +(const
SalesRegister&
> lhs, const SalesRegister& rhs);
>
> and the following definition in the implementation file:
>
>
>
> const SalesRegister operator +(const SalesRegister& lhs, const
> SalesRegister& rhs)
>
> {
>
> SalesRegister temp;
>
>
>
> temp.totalCostPrice = lhs.GetTotalCostPrice +
> rhs.GetTotalCostPrice;
>
> temp.totalSellPrice = lhs.GetTotalSellPrice +
> rhs.GetTotalSellPrice;
>
>
>
> return temp;
>
> }
>
>
>
> and I get this error message: error C2804: binary 'operator +' has too
many
> parameters, how is this possible
>
> as a binary operator has two operands?
>
>
>
> please help me, am I possibly missing a declaration somewhere?
>
> rappy
>

I think you have put the correct declaration in the wrong place.

const SalesRegister operator +(const SalesRegister&
lhs, const SalesRegister& rhs);

Is this inside or outside of the class definition of SalesRegister? It
should be OUTSIDE.

john

David Harmon

unread,
Mar 27, 2004, 11:17:53 AM3/27/04
to
On Sat, 27 Mar 2004 23:52:53 +1100 in comp.lang.c++, "Rappy"
<rap...@iprimus.com.au> wrote,

>I want to overload the addition operator, so that it may add two operands.

Start by creating member function
SalesRegister & SalesRegister::operator*=(SalesRegister const &)

then use non-member function

SalesRegister operator +(


const SalesRegister& lhs, const SalesRegister& rhs)
{

SalesRegister result = lhs;
result += rhs;
return result;
}

DaKoadMunky

unread,
Mar 28, 2004, 11:37:05 PM3/28/04
to
>Or you can declare it outside the class like this
>
>class X
>{
> friend X operator+(const X& lhs, const X& rhs);
>};
>
>X operator+(const X& lhs, const X& rhs)
>{
> ...
>}

Would it be equivalent to make operator+ a static member function?

If the same functionality can be achieved is there ever a reason to prefer a
friend function to a static member function or vice-versa?

Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida

red floyd

unread,
Mar 29, 2004, 1:23:12 AM3/29/04
to

Yep. If you make it a friend, you get implicit conversions on the lhs.
operator+ can't be a static member, it would have to be a non-static member, and
then you wouldn't get implicit conversions on the lhs.

0 new messages