英文:
C++ overloading + operator runs into Problem with Constants
问题
在第40行出现了以下错误:
error: no match for 'operator+' (operand types are 'const Vec2' and 'const Vec2')
以下是您的代码:
#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;
}
有趣的是,当您从第37行中的'a'中移除"const"关键字时,一切都正常工作,但我不想通过更改主函数的内容来修复错误。我对运算符重载还不熟悉,所以感谢您的帮助。
英文:
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.
答案1
得分: 2
The left operand, a
, is const
but your operator+
is not const
qualified.
You simply need to make it so. Also, instead of taking the right operand, b
, by value, you can (optionally) take it by const
reference:
Vec2 operator+(const Vec2& v) const {
// ^^^^^
An alternative is to only define operator+=
as a member function and then to make operator+
a free function:
class Vec2 {
public:
//...
Vec2& operator+=(const Vec2& v) { // this changes *this, so not const qual.
X += v.X;
Y += v.Y;
return *this;
}
};
Vec2 operator+(const Vec2& lhs, const Vec2& rhs) {
Vec2 result(lhs);
result += rhs; // uses the member function operator+=
return result;
}
Alternatively:
Vec2 operator+(Vec2 lhs, const Vec2& rhs) {
return lhs += rhs; // uses the member function operator+=
}
英文:
The left operand, a
, is const
but your operator+
is not const
qualified.
You simply need to make it so. Also, instead of taking the right operand, b
, by value, you can (optionally) take it by const
reference:
Vec2 operator+(const Vec2& v) const {
// ^^^^^
An alternative is to only define operator+=
as a member function and then to make operator+
a free function:
class Vec2 {
public:
//...
Vec2& operator+=(const Vec2& v) { // this changes *this, so not const qual.
X += v.X;
Y += v.Y;
return *this;
}
};
Vec2 operator+(const Vec2& lhs, const Vec2& rhs) {
Vec2 result(lhs);
result += rhs; // uses the member function operator+=
return result;
}
Alternatively:
Vec2 operator+(Vec2 lhs, const Vec2& rhs) {
return lhs += rhs; // uses the member function operator+=
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论