In C++, how are the operator overloading functions distinguished for the unary and binary minus operators?
I am trying to overload both with the following code:
Vector Vector::operator-(){
return Vector(-x,-y,-z);
}
Vector Vector::operator-(const Vector& v){
return this* + (-v);
}
But this spews a lot of errors:
vector.cpp: In member function ‘Vector Vector::operator-(const Vector&)’:
vector.cpp:88:20: error: passing ‘const Vector’ as ‘this’ argument of ‘Vector Vector::operator-()’ discards qualifiers [-fpermissive]
return this* + (-v);
^
vector.cpp:88:16: error: no match for ‘operator+’ (operand type is ‘Vector’)
return this* + (-v);
^
vector.cpp:88:16: note: candidates are:
vector.cpp:70:8: note: Vector Vector::operator+(const Vector&)
Vector Vector::operator+(const Vector& v){
^
...
How do I fix this problem?
constmember function, i.e.Vector Vector::operator-(const Vector&) const;and Vector Vector::operator-() const;`