The other day, I ran into trouble when I tried to overload a two-parameter operator with the use of a class member function. I tried references, but nothing changed. The compiler said I couldn't write a member function that takes more than one argument of the same type as the class itself. Why is that?
Here is the code:
class Fraction
{
public:
Fraction(int num=1, int den=1): numerator(num), denominator(den) {}
Fraction(const Fraction& r): numerator(r.numerator), denominator(r.denominator) {}
Fraction& operator=(const Fraction&);
Fraction& operator*(const Fraction&, const Fraction&);
private:
int numerator, denominator;
};
Fraction& Fraction::operator=(const Fraction& r)
{
numerator = r.numerator;
denominator = r.denominator;
return *this;
}
Fraction Fraction::operator*(const Fraction& x, const Fraction& y)
{
Fraction z(x.numerator*y.numerator, x.denominator*y.denominator);
return z;
}
The following is the error message from the compiler:
Fraction& Fraction::operator*(const Fraction&, const Fraction&)' must take either zero or one argument