0

When I try compiling a CPP file that contains the following class:

class Point {
public:
    Point(int x = 1, int y = 1): x(x), y(y) {}

    Point operator+(const Point& p1, const Point& p2)
    {
        /* some code */
    }

private:
    int x;
    int y;
};

I get the error: must take either zero or one argument. Actually I've found some similar questions but I couldn't get the answer that made me understand the reason behind that. Why is it forbidden to give it two arguments and why is it legal when we declare the function as friend?

alexmoran
  • 21
  • 1
  • How would you call an operator like `+` with 3 arguments? The first argument is the `this` pointer if it's a member method. If it's declared as `friend` no `this` pointer is used as first argument. – Thomas Sablik Mar 27 '20 at 17:39
  • maybe it helps to consider that `x + y` is `x.operator+(y)` (when implemented as member function). If it would be allowed, your operator would be `x.operator+(y,???)` – 463035818_is_not_an_ai Mar 27 '20 at 17:57

0 Answers0