I defined a Vec2 object that should represent a 2d-vector. I tried to overload the + operator to implement vectoraddition but I get following error in Line 40:
error: no match for 'operator+' (operand types are 'const Vec2' and 'const Vec2')
Here's my code:
#include <iostream>
using namespace std;
class Vec2 {
private:
double X;
double Y;
public:
Vec2(double x, double y){
X = x;
Y = y;
}
double getX() const {
return X;
}
double getY() const {
return Y;
}
Vec2 operator+(Vec2 v ){
Vec2 res(v.getX()+X, v.getY()+Y);
return res;
}
};
ostream& operator<<(ostream& s , const Vec2& my_vec)
{
s << " ( "<< my_vec.getX()<< " , "<< my_vec.getY() <<" ) " ;
return s ;
}
int main()
{
Vec2 const a(3,4); cout << a << endl;
Vec2 const b(3,4); cout << b << endl;
Vec2 c(a+b); cout << c << endl;
return 0;
}
Interestingly when you remove the "const" keyword from a in line 37 everything works, but I don't wan't to fix the error by changing the content of the main function. I am new to operator overloading, so thanks for the help.