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

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

C++ overloading + operator runs into Problem with Constants

问题

在第40行出现了以下错误:

  1. error: no match for 'operator+' (operand types are 'const Vec2' and 'const Vec2')

以下是您的代码:

  1. #include <iostream>
  2. using namespace std;
  3. class Vec2 {
  4. private:
  5. double X;
  6. double Y;
  7. public:
  8. Vec2(double x, double y){
  9. X = x;
  10. Y = y;
  11. }
  12. double getX() const {
  13. return X;
  14. }
  15. double getY() const {
  16. return Y;
  17. }
  18. Vec2 operator+(Vec2 v ){
  19. Vec2 res(v.getX()+X, v.getY()+Y);
  20. return res;
  21. }
  22. };
  23. ostream& operator<<(ostream& s , const Vec2& my_vec)
  24. {
  25. s << " ( " << my_vec.getX()<< " , " << my_vec.getY() << " ) ";
  26. return s ;
  27. }
  28. int main()
  29. {
  30. Vec2 const a(3,4); cout << a << endl;
  31. Vec2 const b(3,4); cout << b << endl;
  32. Vec2 c(a+b); cout << c << endl;
  33. return 0;
  34. }

有趣的是,当您从第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:

  1. error: no match for &#39;operator+&#39; (operand types are &#39;const Vec2&#39; and &#39;const Vec2&#39;)

Here's my code:

  1. #include &lt;iostream&gt;
  2. using namespace std;
  3. class Vec2 {
  4. private:
  5. double X;
  6. double Y;
  7. public:
  8. Vec2(double x, double y){
  9. X = x;
  10. Y = y;
  11. }
  12. double getX() const {
  13. return X;
  14. }
  15. double getY() const {
  16. return Y;
  17. }
  18. Vec2 operator+(Vec2 v ){
  19. Vec2 res(v.getX()+X, v.getY()+Y);
  20. return res;
  21. }
  22. };
  23. ostream&amp; operator&lt;&lt;(ostream&amp; s , const Vec2&amp; my_vec)
  24. {
  25. s &lt;&lt; &quot; ( &quot;&lt;&lt; my_vec.getX()&lt;&lt; &quot; , &quot;&lt;&lt; my_vec.getY() &lt;&lt;&quot; ) &quot; ;
  26. return s ;
  27. }
  28. int main()
  29. {
  30. Vec2 const a(3,4); cout &lt;&lt; a &lt;&lt; endl;
  31. Vec2 const b(3,4); cout &lt;&lt; b &lt;&lt; endl;
  32. Vec2 c(a+b); cout &lt;&lt; c &lt;&lt; endl;
  33. return 0;
  34. }

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:

  1. Vec2 operator+(const Vec2& v) const {
  2. // ^^^^^

Demo


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

  1. class Vec2 {
  2. public:
  3. //...
  4. Vec2& operator+=(const Vec2& v) { // this changes *this, so not const qual.
  5. X += v.X;
  6. Y += v.Y;
  7. return *this;
  8. }
  9. };
  10. Vec2 operator+(const Vec2& lhs, const Vec2& rhs) {
  11. Vec2 result(lhs);
  12. result += rhs; // uses the member function operator+=
  13. return result;
  14. }

Demo


Alternatively:

  1. Vec2 operator+(Vec2 lhs, const Vec2& rhs) {
  2. return lhs += rhs; // uses the member function operator+=
  3. }
英文:

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:

  1. Vec2 operator+(const Vec2&amp; v) const {
  2. // ^^^^^

Demo


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

  1. class Vec2 {
  2. public:
  3. //...
  4. Vec2&amp; operator+=(const Vec2&amp; v) { // this changes *this, so not const qual.
  5. X += v.X;
  6. Y += v.Y;
  7. return *this;
  8. }
  9. };
  10. Vec2 operator+(const Vec2&amp; lhs, const Vec2&amp; rhs) {
  11. Vec2 result(lhs);
  12. result += rhs; // uses the member function operator+=
  13. return result;
  14. }

Demo


Alternatively:

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

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:

确定