你好我有如下所示的代码我的编译错误。我真的不知道为什么,这就是为什么我在这里。谁能帮我纠正我的错误?新来运算符重载。提前致谢。
这是我收到的编译错误:
//Point.cpp:45:11:错误:构件的分配“CS170 ::点:: X”在只读对象
X + = other.x; (.X被高亮显示)
//Point.cpp:117:12:错误:在只读对象构件的作业“CS170 ::点:: Y”
Y = Y-other.y; (。而被高亮显示)
//Point.cpp:47:9:错误:绑定类型的参考“CS170 ::点和”到“const的CS170 ::点”丢弃限定符
返回*本;(*此被高亮显示)
//Point.cpp:58:13:错误:不能类型的非const左值参考“CS170 ::点&”结合类型的右值“CS170 ::点”
return Point(pow(x,other.x),pow(y,other.y));
(点(POW(x.other.x),功率(Y,other.y)被高亮显示)
Point.cpp:在成员函数“CS170 ::点和CS170 ::点::运算%(双)”:Point.cpp:94:5:错误:类型“双”和“双”二进制“运算符的操作数无效%”
X = X%值;(被突出显示X%值)
//Point.cpp:143:41:错误:没有“诠释CS170 ::点::乘(常量CS170 ::点和)”在类中声明成员函数“CS170 ::点”
INT点::乘(常量点及其他)
//Point.cpp:76:31:错误:未使用的参数“虚设” [-Werror =未使用的参数]
点与点::运算符 - (INT假)(假人高亮)
#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);
}
}
你需要做的operator+=
成员函数非const
成员函数。
Point& Point::operator+=(const Point& other) // No const
{
x += other.x;
y += other.y;
return *this;
}
函数修改该函数被调用的对象。它没有任何意义,使之成为const
成员函数。
operator+
函数需要返回一个对象,而不是一个对象的引用。+=
操作被简化。这里有一个更新的版本。
Point Point::operator+(const Point& other) const
{
Point ret(*this);
return (ret += other);
}
该operator^
函数需要返回一个对象,而不是引用。
Point Point::operator^(const Point& other)
{
return Point(pow(x,other.x),pow(y,other.y));
}
当您使用return Point(...);
,它不能是一个对象的引用。