binary 'operator *' has too many parameters 
Author Message
 binary 'operator *' has too many parameters

class Rational
{
public:
 const Rational operator *(const Rational & lhs,
  const Rational & rhs);
...

Quote:
}

Why does the above code generate an error?

--
If you can help, or suggest somewhere else I should post this message, then
please email me at

Thanks,
Ian Sweeney
http://www.*-*-*.com/ ~ian_sweeney
ICQ - 19354698



Wed, 07 May 2003 03:00:00 GMT  
 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


Wed, 07 May 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. error C2676: binary '+' : 'class' does not define this operator

2. Comma operator in #define's (was Re: Usage of comma operator)

3. System::String '==' operator

4. '...' operator

5. VB activation: CreateObject Vs 'New' operator

6. Equality '==' Operator

7. error C2593: 'operator <' is ambiguous

8. 'operator =' for CArray derived class

9. 'operator <<' is ambiguous error

10. Defining operator '=' for CMyClass

11. Protected inheritance and the 'new' operator

12. CString derived class: operator '=' error

 

 
Powered by phpBB® Forum Software