【问题标题】:Overloading operators with compilation error重载带有编译错误的运算符
【发布时间】:2019-06-27 05:12:53
【问题描述】:

您好,我的代码出现编译错误,如下所示。我真的不知道为什么这就是我在这里的原因。谁能帮我纠正我的错误?运算符重载的新手。提前致谢。

这是我收到的编译错误:

//Point.cpp:45:11: 错误:只读对象中成员‘CS170::Point::x’的赋值

x+=其他.x; (.x 突出显示)

//Point.cpp:117:12: 错误:只读对象中成员‘CS170::Point::y’的赋值

y=y-other.y; (.y 突出显示)

//Point.cpp:47:9: 错误:将‘CS170::Point&’类型的引用绑定到‘const CS170::Point’丢弃限定符

return *this;(*this 被高亮显示)

//Point.cpp:58:13: 错误:无法将‘CS170::Point&’类型的非常量左值引用绑定到‘CS170::Point’类型的右值

  return Point(pow(x,other.x),pow(y,other.y)); 

(Point(pow(x.other.x),power(y,other.y) 高亮显示)

Point.cpp:在成员函数‘CS170::Point& CS170::Point::operator%(double)’中: Point.cpp:94:5: 错误:“double”和“double”类型的无效操作数到二进制“operator%”

x=x%value;(x%value 被突出显示)

//Point.cpp:143:41: 错误:没有在类‘CS170::Point’中声明的‘int CS170::Point::Multiply(const CS170::Point&)’成员函数

int Point::Multiply(const Point& other)

//Point.cpp:76:31: 错误:未使用的参数'dummy' [-Werror=unused-parameter]

Point& Point::operator--(int dummy) ( dummy 高亮显示)

#include "Point.h"  

#include <cmath>    

namespace CS170

{

    const double PI = 3.1415926535897;

    const double EPSILON = 0.00001;

/////////////////////////////////////// //////////////////////////// // 私有成员函数

double Point::DegreesToRadians(double degrees) const
{;

    return (degrees * PI / 180.0);

}

double Point::RadiansToDegrees(double radians) const
{

    return (radians * 180.0 / PI);

}

/////////////////////////////////////// //////////////////////////// // 16 个公共成员函数(2 个构造函数,14 个运算符)

Point::Point(double X, double Y): x(X), y(Y) { }


Point::Point(){

    x=0.0f;

    y=0.0f;

}
Point& Point::operator+(double value)
{

    x=x+value;

    y=y+value;

    return *this;

}
Point& Point::operator+(const Point& other) const
{

    x+=other.x; 

    y+=other.y;

    return *this;

}

Point& Point::operator-(const Point& other)
{

    return -(other.x), -(other.y) ;

}
Point& Point::operator^(const Point& other) 
{

    return Point(pow(x,other.x),pow(y,other.y));

}
Point& Point::operator++(int dummy)
{

    x++;

    y++;

    return *this;

}

Point& Point::operator++()
{

    ++x;

    ++y;

    return *this;

}

Point& Point::operator--(int dummy)
{

    x--;

    y--;

    return *this;

}

Point& Point::operator--()
{
    --x;

    --y;

    return *this;

}

Point& Point::operator%(double value)
{

    x=x%value;

    y=y%value;

    return *this;

}

Point& Point::operator+=(const Point& other) const
{

    x += other.x;

    y += other.y;

    return *this;

}

Point& Point::operator+=(double value)
{

    return Point(x+value,y+value);

}
Point& Point::operator-(int value)
{

    return Point(x-value,y-value);

}

Point& Point::operator-(const Point& other) const

{

    x=x-other.x;

    y=y-other.y;

// return Point(x-other.x,y-other.y);
    return *this;

}

Point& Point::operator*(const Point& other) const
{

    return Multiply(other);

}

/////////////////////////////////////// //////////////////////////// // 2 个友元函数(运算符)

std::ostream& operator<< (std::ostream &output, const Point &point)
    { 

        output << "(" << point.x << "," << point.y << ")";

        return output;

    }   

std::istream& operator>>(std::istream &input, Point &point ) 
    { 

        input >> point.x >> point.y;

        return input;            

    }

/////////////////////////////////////// //////////////////////////// // 2 个非成员,非朋友(操作员)

int Point::Multiply(const Point& other) 
{

    return Point(x*other.x, y*other.y);

}
int Point::Add(const Point& other) 
{           

    return Point(x+other.x, y+other.y);

}


}

【问题讨论】:

  • 你明白方法签名末尾的const是什么意思吗?
  • operator + 应该返回一个全新的 Point 对象,而不是对原始对象的引用。与operator - 相同。如果您想返回对原始文件的引用,那是 operator +=operator -= 应该做的。
  • 无关:the great tome of wisdom 与运算符重载有关。

标签: c++ operator-overloading


【解决方案1】:

问题 1

您需要将operator+= 成员函数设为非const 成员函数。

Point& Point::operator+=(const Point& other)  // No const
{
    x += other.x;

    y += other.y;

    return *this;    
}

函数修改调用函数的对象。将其设为const 成员函数是没有意义的。

问题 2

  • operator+ 函数需要返回一个对象,而不是对对象的引用。
  • 需要更改其实现,以便修改当前对象。
  • 可以使用+= 运算符简化实现。

这是一个更新的版本。

Point Point::operator+(const Point& other) const
{
    Point ret(*this);
    return (ret += other);
}

问题 3

operator^ 函数需要返回一个对象,而不是一个引用。

Point Point::operator^(const Point& other) 
{
    return Point(pow(x,other.x),pow(y,other.y));
}

当你使用return Point(...);时,它不能是一个对象的引用。

【讨论】:

  • @playpro0 建议:在编译和测试之前少写代码,最多一个函数。您可以更快地发现错误并且不太可能重复它。此外,一个虫子通常很容易找到和修复,但两个虫子可以互相取食并隐藏起来,这使得狩猎变得更加困难。不要给他们时间来建立。
  • 好的,我明白了。顺便说一句,我的++重载代码是否正确?就后缀和前缀而言。只是想仔细检查一下。
  • 无关:在 C++ 中 ^ 是 XOR 运算符。将其用于求幂可能会导致混淆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多