error: lvalue required as left operand of assignment

1
2
3
4
5
6
7
Point2D Point2D::operator+(Point2D param) 
{
  Point2D temp;
  temp.getX() = getX() + param.getX();
  temp.getY() = getY() + param.getY();
  return (temp);
}


I'm learning about operator overloading and coded this. I have a getX() and getY() in my .h file. However it keep on prompting error as topic stated.. help on this pls
Your getX() apparently returns a value, it's probably defined somehow like

double Point2D::getX() const { return x; }

In order to assign to getX(), it needs a version that returns a reference:

double& Point2D::getX() { return x; }

although it's questionable design. Why not have Point2D's constructor take two arguments: x, and y, and write

Point2D temp(getX() + param.getX(), getY() + param.getY());
Last edited on
Since point2D::operator+ is a part of the Point2D class, you should be able to directly access 'x' and 'y':

temp.x = getX() + param.getX();
Cubbi's method is still a lot cleaner though. If you're going to assign values to a new Object, you're better off doing it in the constructor.
Is there a way to do without pointers and reference as I have no clue about them...

i changed my code to this:
1
2
3
4
5
6
7
Point2D Point2D::operator+(Point2D param) 
{
  Point2D temp;
  temp.x = getX() + param.getX();
  temp.y = getY() + param.getY();
  return (temp);
} 
however, my error now states this...

/tmp/ccACVakl.o: In function `Line3D::Line3D()':
main.cpp:(.text+0x59a): undefined reference to `vtable for Line3D'
/tmp/ccACVakl.o: In function `Line3D::Line3D(Point3D, Point3D)':
main.cpp:(.text+0x62a): undefined reference to `vtable for Line3D'
main.cpp:(.text+0x693): undefined reference to `Line3D::setLength()'
collect2: ld returned 1 exit status
Okay i figured out. solved
Topic archived. No new replies allowed.