0

I have a Point class that represents a point in 2d plane. I have written a simple operator + member function . I am able to add two objects of the class Point , but not three. Why cant I add 3 objects in one line? The following code works perfectly -:

Point p(1,2) , q(3,4);
Point r = p+q;
cout<<p+q;

The following code gives an error;

Point w = p+q+r;

Error - ‘Point’ is not derived from ‘const std::reverse_iterator<_Iterator>’

Below is my implementation

class Point{

 int x;
 int y;
 static int count ; 

public:
Point() :x(0) , y(0) {count++;}
Point(int x,int y){this->x = x; this->y =y; count++;}
~Point(){    count--; }

int getx() { return x ; }
int gety() { return y ; } // can const objects call these functions.. check
void setx( int x) { this->x = x;}
void sety( int y) { this->y = y;}

friend Point operator +(Point &, Point &); 
friend std::ostream & operator << (std::ostream &,Point );
};


int Point::count = 0;

Point operator +(Point &p,Point &q) // check if const p can be passed
{
    Point sum(q.getx() + p.getx(), q.gety() + p.gety());
    return sum;
}

std::ostream & operator << (std::ostream &os,Point p)
{
    os<<"("<<p.getx()<<","<<p.gety()<<")";
    return os;

}
  • Have you seen [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/operator-overloading-in-c/4421719#4421719) The prototype shown there for `operator+` does not match what you have... (keyword: `const`) – JaMiT May 05 '22 at 03:58
  • Your `operator+` takes a `Point&` by non-`const` lvalue reference. An rvalue like the result of adding two `Point`s [cannot bind to that](https://stackoverflow.com/questions/51836609/why-can-an-rvalue-not-bind-to-a-non-const-lvalue-reference-other-than-the-fact). You can fix this by making it take a `const Point&`, which also more clearly documents that your function will not be changing the value of the `Point`s. – Nathan Pierson May 05 '22 at 03:59
  • Based on the comments in your example code, the following might be worth reading: [A: Sell me on const correctness](https://stackoverflow.com/a/137071) – JaMiT May 05 '22 at 04:05
  • Thanks @NathanPierson I fixed it by making my making get functions const and operator + taking `const Point &` – Srikanth S May 05 '22 at 04:29

1 Answers1

-2

An operator+ cannot be overloaded for three parameters. If you want to add three classes together you need a separate function of the Three parameters of the class data type. Like :

Point sum(Point&a , Point&b , Point&c)
{
 Point sum ;
 int x= a.getx() + b.getx() + c.getx() ;
 sum.setx(x);
 int y= a.gety() + b.gety() + c.gety() ;
 sum.sety(y);
 return sum;
 }
  • This code at least can work, but it's still not a very good solution to the problem. What if you want to add four `Point`s? Ten? Do you break out the parameter packs? It's much simpler to change the code so that `a + b + c` being handled as `(a + b) + c` just works via two separate applications of binary `operator+`. – Nathan Pierson May 05 '22 at 04:51