C++中重载+运算符时遇到常量的问题。

huangapple go评论65阅读模式
英文:

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 &#39;operator+&#39; (operand types are &#39;const Vec2&#39; and &#39;const Vec2&#39;)

Here's my code:

#include &lt;iostream&gt;

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&amp; operator&lt;&lt;(ostream&amp; s , const Vec2&amp; my_vec)
{
s &lt;&lt; &quot; ( &quot;&lt;&lt; my_vec.getX()&lt;&lt; &quot; , &quot;&lt;&lt; my_vec.getY() &lt;&lt;&quot; ) &quot; ;
return s ;
}

int main()
{
    Vec2 const a(3,4);      cout &lt;&lt; a &lt;&lt; endl;
    Vec2 const b(3,4);      cout &lt;&lt; b &lt;&lt; endl;

    Vec2 c(a+b);       cout &lt;&lt; c &lt;&lt; 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 {
//                            ^^^^^

Demo


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;
}

Demo


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&amp; v) const {
//                            ^^^^^

Demo


An alternative is to only define operator+= as a member function and then to make operator+ a free function:

class Vec2 {
public:
//...
    Vec2&amp; operator+=(const Vec2&amp; v) { // this changes *this, so not const qual.
        X += v.X;
        Y += v.Y;
        return *this;
    }
};

Vec2 operator+(const Vec2&amp; lhs, const Vec2&amp; rhs) {
    Vec2 result(lhs);
    result += rhs;     // uses the member function operator+=
    return result;
}

Demo


Alternatively:

Vec2 operator+(Vec2 lhs, const Vec2&amp; rhs) {
    return lhs += rhs; // uses the member function operator+=
}

huangapple
  • 本文由 发表于 2023年6月12日 23:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定