operator+ overload

Hello,
I am trying to overload the "plus" operator for a class I wrote. Now, I set a namespace with some const objects of my class. Here an example


1
2
3
4
5
6
7
8
9
10
11
class myVector{
public: 
//...
myVector operator+(const myVector &rhs){
myVector result; 
result.x = this->x + rhs.x;
result.y = this->y + rhs.y;

return result;
}


Despite the fact that the code works nicely if I sum up two variable vectors u+v, the compiler complaints if I try to sum two const vector.

How can I solve this issue?
myVector operator+(const myVector &rhs) const
You can't call a method of a constant object if it doesn't have the const specifier

1
2
3
4
5
6
7
myVector operator+(const myVector &rhs) const{
myVector result; 
result.x = this->x + rhs.x;
result.y = this->y + rhs.y;

return result;
}
Topic archived. No new replies allowed.