
binary 'operator *' has too many parameters
Quote:
> class Rational
> {
> public:
> const Rational operator *(const Rational & lhs,
> const Rational & rhs);
> ...
> }
> Why does the above code generate an error?
When you define a binary operator as a member function
it must have one parameter. An instance of the object
works as the first and the only parameter as the second
argument to the operator. In the body of the function you
access the instance through the _this_ pointer.
class Rational
{
public:
const Rational operator * (const Rational & rhs);
...
Quote:
};
If you need your operator be simmetric you define it as
a regular function out of the class's scope. Usually you
need to access the class's private data and have to
declare the function as a friend.
class Rational
{
friend const Rational operator * (const Rational & lhs, const Rational & rhs);
public:
...
Quote:
};
const Rational operator * (const Rational & lhs, const Rational & rhs)
{
...
Quote:
}
Sergei