declaration in class:
const SalesRegister operator +(const SalesRegister& lhs, const
SalesRegister& rhs);
definition in implementation file:
const SalesRegister SalesRegister::operator+(const SalesRegister
¶m)
{
.....
.....
}
thank you in advance
rappy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
> 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
> ¶m)
> {
>
Hi!
If you choose to implement the operator+ as member function, the left
operand to the operator is the actual object (this). If you choose to
implement it as an external function, you need both operands as parameters.
An example:
class SalesRegister
{
public:
// operator+ overloaded as member function
SalesRegister operator +(const SalesRegister& rhs) const;
// operator- overloaded as external function
friend SalesRegister operator -(const SalesRegister& lhs,
const SalesRegister& rhs);
};
// operator + overloaded as member function
SalesRegister SalesRegister::operator +(const SalesRegister& rhs) const
{
SalesRegister temp(*this);
// ... add rhs to temp --> temp+= rhs
return temp;
}
// operator - overloaded as external function
SalesRegister operator -(const SalesRegister& lhs,
const SalesRegister& rhs)
{
SalesRegister temp(lhs);
// ... subtract rhs from temp --> temp-= rhs;
return temp;
}
Hope this helps, Johann
The operator + as a non-static member function takes a single parameter. The
operator + as a static member function takes two parameters. In your example
above you are declaring operator + as a non-static member function taking
two parameters and defining it taking a single parameter, which is obviously
not correct.
> 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
> ¶m)
You basically have two options how you overload binary operator+ for class
SalesRegister:
- as a member function
- as a non-member function
As a member function, operator+() has a parameter list of size 1. If you
have
SalesRegister sr1;
SalesRegister sr2;
, the expression
sr1+sr2
the operator can access sr2 through that one parameter; sr1 can be accessed
through the *this pointer.
As a non-member function, operator+() ha a parameter list of size 2. The
operator can access sr1 through the first parameter and sr2 through the second.
The code of yours I quoted above is self-contradictory. The declaration of the
operator lists two parameters (as if you intended to overload the operator as
a non-member), but the definition lists only one parameter (as if you
intended to overload it as a member).
Correct solutions are
- as a member function:
class SalesRegister
{
public:
SalesRegister operator+(SalesRegister const &) const;
};
SalesRegister SalesRegister::operator+(SalesRegister const &sr) const
{
return /* the sum of *this and sr */;
}
[Note that I declared the member function const to guarantee to the caller
that the lefthand argument is not modified by operator+().]
- as a non-member function
class SalesRegister
{
public:
friend SalesRegister operator+(SalesRegister const &,
SalesRegister const &);
};
SalesRegister operator+(SalesRegister const &sr1,
SalesRegister const &sr2)
{
return /* the sum of sr1 and sr2 */;
}
Strictly speaking, it's not necessary to declare the operator as a friend
unless it has to access non-public members of SalesRegister.
The non-member solution is often preferred because both operands are treated
equally wrt to implicit conversions.
Are you saying that you've declared operator+ with the above signature
as a member function? If that's the case then that'll be the problem -
if you want to declare operator+ as a class member then the signature
is as follows:
SalesRegister operator+(const SalesRegister &rhs)
As operator+ in this case is a member function, it will be applied to
an existing SalesRegister object which makes up the lhs of the
expression.
The declaration you've used above is the one used when you implement
operator+ as a free function instead of a class member.
[code]
class SalesRegister {
int data_;
public:
SalesRegister(int data): data_(data) {}
const SalesRegister
operator+(const SalesRegister& rightOfThePlus);
};
const SalesRegister
SalesRegister::operator+(const SalesRegister&
rightOfThePlus)
{
SalesRegister retVal(0);
retVal.data_ = data_ + rightOfThePlus.data_;
return retVal;
}
int
main()
{
SalesRegister a(21), b(21), c(0);
c = a + b;
//c contains 42 now.
return 0;
}
[code]
Ciao