[英]Overloading operators with compilation error
嗨,我的代码显示如下编译错误。 我真的不知道为什么这就是为什么我在这里。 谁能帮助我纠正我的错误? 操作员重载的新手。 提前致谢。
这是我收到的编译错误:
//Point.cpp:45:11:错误:只读对象中成员'CS170 :: Point :: x'的分配
X + = other.x; (突出显示.x)
//Point.cpp:117:12:错误:只读对象中成员'CS170 :: Point :: y'的分配
Y = Y-other.y; (.y突出显示)
//Point.cpp:47:9:错误:将类型'CS170 :: Point&'的引用绑定到'const CS170 :: Point'会丢弃限定符
返回* this;(*突出显示)
//Point.cpp:58:13:错误:无法将类型'CS170 :: Point&'的非常量左值引用绑定到类型'CS170 :: Point'的右值
return Point(pow(x,other.x),pow(y,other.y));
(点(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)(虚拟突出显示)
#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(...);
,它不能是对对象的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.