Question

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

Was it helpful?

Solution 2

Firstly, the issue you are observing has absolutely nothing to do with member functions in general. Member functions can generally take arbitrary number of arguments of "same class" type. In your example you can declare

class Fraction
{
   void foo(Fraction &f1, Fraction &f2, Fraction &f3, Fraction &f4) {}
   ...
};

without any problems. So, it is not clear why you decided to word your question as if it is about member functions in general.

Secondly, in your code it is really about the simple fact that you are trying to overload an operator. Operator syntax in C++ is fixed for most (but not all) operators. This immediately means that those operators whose syntax is fixed will have fixed number of parameters.

In your example, it is operator *. It can be unary (one parameter) or binary (two parameters). When you overload this operator by a member function one parameter is already implied, so you can only add zero or one additional parameters (for unary * and binary * respectively). You, on the other hand, are trying to add two more parameters in additions to the implicit one. I.e. you are trying to define a ternary operator *. This is not possible. There's no ternary * operator in C++. And this is exactly what the compiler is telling you.

OTHER TIPS

The operators take a fixed amount of arguments, for example operator+ as the addition operator takes exactly 2 parameters. If it is a member function the first(leftmost) is implied to be this and the second is passed as a parameter.

What would calling an operator+ that took three parameters look like? One might imagine it would look like 3 + 4 + 5 but that is the equivalent of calling operator+(3,4) then operator+(7,5).

For a list of operators and how many arguments they take please check out wikipedia: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

For more details there is another StackOverflow post that goes into great detail: Operator overloading

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top