在过载operator<
方面遇到困难。我试图超载operator<
,以便可以根据pt1
的x
对Line2D
对象进行排序。但是我很难弄清楚如何声明该功能。
我遇到了错误:
object has type qualifiers that are not compatible with the member function "Point2D::getX"
。
我尝试的是:删除const
,将Point2D &l2dobj
放置。
class Line2D
{
private:
Point2D pt1;
Point2D pt2;
public:
bool operator<( const Line2D &l2dobj)
{
return (pt1.getX() < l2dobj.pt1.getX());
}
}
class Point2D
{
protected:
int x;
int y;
public:
int getX();
int getY();
}
point2d :: getx 不接受 const 实例,您不能在 l2dobj 上应用它它是 const Reference ,更改 getx (和先验 gety )to:
class Point2D
{
protected:
int x;
int y;
public:
int getX() const;
int getY() const;
};
作为一种通用方式,声明方法 const 您可以越多,并且它们的参数相同