1

我已经定义了一个类点。我还有一个类 PointCollection :class PointCollection: public QVector<Point>在实现某些方法时,我收到以下错误:

错误:'operator==' 不匹配(操作数类型为 'Point' 和 'const Point')

这是我遇到此错误的代码部分:

    Point PointCollection::getNearestPointToCentroid()
{
    float minDist = 0.0;
    int NearestPointToCentroidIndex = -1;
    while(!this->empty())
    {
        Point point;
        Point centroid;
        float dist = PointT.calculateEuclideanDist(point, centroid);
        if(this->indexOf(point) == 0)
        {
            minDist = dist;
            NearestPointToCentroidIndex = this->indexOf(point);
        }
        else
        {
            if(minDist > dist)
            {
                minDist = dist;
                NearestPointToCentroidIndex = this->indexOf(point);
            }
        }
    }
    return(this[NearestPointToCentroidIndex]);
}

在哪里:Point centorid;float X;float Y;int Id;是 PointCollection 类的私有变量。在构造函数中我定义:

PointCollection::PointCollection()
{
    //centorid = new Point;
    Id = PointT.GetId();
    X = PointT.GetX();
    Y = PointT.GetY();
}

float Point::calculateEuclideanDist(Point point_1, Point point_2)
{
    float x1 = point_1.x, y1 = point_1.y;
    float x2 = point_2.x, y2 = point_2.y;

    float dist = qSqrt(qPow(x2 - x1, 2.0) + qPow(y2 - y1, 2.0));


    return (dist);
 }
4

1 回答 1

2

问题是为了实现 indexOf,QVector 必须知道如何比较 Points 是否相等(否则它如何找到向量中的点)。它为此使用 operator==,但您还没有为类 Point 编写 operator==,因此您会收到此错误。只需为 Point 编写 operator== (并且 operator!= 也是一个好主意)。

bool operator==(const Point& x, const Point& y)
{
    // your code here
}

bool operator!=(const Point& x, const Point& y)
{
    return !(x == y);
}
于 2013-08-20T18:38:12.937 回答