“2 overloads have no legal conversion for 'this' pointer”错误

本文探讨了在C++编程中遇到的'2 overloads have no legal conversion for 'this' pointer'错误。该错误源于尝试使用const对象、const指针或const引用调用非const成员函数。解决方案是去除operator+=函数中const修饰符,以允许正确调用。
摘要由CSDN通过智能技术生成

“2 overloadshave no legal conversion for 'this' pointer”错误

——《深度探索C++对象模型》读书札记系列

 

问题:P101的代码,如下,编译出现“2 overloads have nolegal conversion for 'this' pointer”错误。

class Point2d {
public:
	Point2d(float x = 0.0, float y = 0.0) 
		: _x(x), _y(y) {};

	float x() { return _x; }
	float y() { return _y; }

	void x(float newX) { _x = newX; }
	void y(float newY) { _y = newY; }

	void operator+=(const Point2d& rhs)
	{
		_x += rhs.x();
		_y += rhs.y();
	}
protected:
	float _x, _y;
};

class Point3d : public Point2d {
public:
	Point3d(float x = 0.0, float y = 0.0, float z = 0.0) 
		: Point2d(x, y), _z(z) {};

	float z() { return _z; }

	void z(float newZ) { _z = newZ; }

	void operator+=(const Point3d& rhs)
	{
		Point2d::operator+=(rhs);
		_z += rhs.z();
	}
protected:
	float _z;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值