【发布时间】: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 与运算符重载有关。