well for starters... do you really need get/set fucntions for x and y or are those useless getters/setters?
My guess is they're useless,
so I would get rid of them and just make x and y public. There's nothing wrong with public members where appropriate. Useless getters/setters are a waste of time and energy and make the interface weird.
But if they're useful, the solution is to make get_x and get_y const:
1 2 3 4
|
//int two_vector::get_x(); // bad
int two_vector::get_x() const; // good
// same for get_y()
|
Also, for the + operator, whats the purpose of the const at the end of the prototype? |
It makes the function const.
const functions "promise" not to change the state of the object. Therefore they can be called by const objects.
non-const functions might change the state of the object, therefore they can only be called by nonconst objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class example
{
public:
void nonconst() { } // a non-const function
void yesconst() const { } // a const function
};
int main()
{
example a;
a.nonconst(); // OK
a.yesconst(); // OK
const example b;
b.nonconst(); // ERROR, const object calling nonconst function
b.yesconst(); // OK
}
|
Similarly, const functions can only call other const functions. This is the error you were getting. Your + operator (const) was trying to call get_x (nonconst). Since get_x might change the state of the object, this breaks the "promise", and therefore the compiler barks at you.
EDIT:
I just realized you posted the entire class above!
Yes, get_x/get_y/set_x/set_y are useless getters/setters. Get rid of them. x and y should be public for this class.