Question

I have defined a class Point. I also have a class PointCollection : class PointCollection: public QVector<Point> Here when implementing some methods I get following error:

error: no match for 'operator==' (operand types are 'Point' and 'const Point')

Here is the code part on which I have this error:

    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]);
}

Where: Point centorid;float X;float Y;int Id; are private variables of PointCollection class. In constructor I define :

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

And

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);
 }
Was it helpful?

Solution

The problem is that in order to implement indexOf, QVector has to know how to compare Points for equality (otherwise how can it find the point in the vector). It uses operator== for this, but you haven't written operator== for class Point, so you get this error. Just write operator== for Point (and operator!= would be a good idea too).

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

bool operator!=(const Point& x, const Point& y)
{
    return !(x == y);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top