No match for 'operator==' (operand types are 'Point' and 'const Point')
-
Hi,
here is some part of my code, where Point is a class defined by me:
@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]);
}@Can anyone help me solving this?
-
It is very easy.
your comparing a point to a const point. The == operator is not capable of doing so, thus the error. Why it does this is the biggest question ;-)
If I read your code I assume you subclassed a QList? Or did you write your own empty() and indexOf() functions? First of all I want to stress out that doing this is highly discouraged! This to do with the fact that QList has no public destructor and when your class is destroyed it will leave element in the QList in the memory leaving a memory leak that will cause problems later on. So do NOT subclass a QList (although it's very handy and I did it before aswell and bit me in the ass later on). Check google if you don't believe it.
On the error it is helpfull if you point out what line is crashing and add your class definition to it. There might be something in there.
And what should this code do??? If I may guess, add some sort of point to your QList of points that is the closes to an other point. And where does PointT comes from? -
Hi, thanks for the answer.
I work not with QList, but with QVector. I want to do kmeans that's why I defined some Point class (see source in the end of the post), and made an object of it in another class. And the function might have to get the nearest point to the centroid of the cluster.Here are the sources:
@#ifndef POINT_H
#define POINT_H#include <iostream>
#include <string>
#include <qmath.h>using namespace std;
class Point
{public:
Point();
Point(float, float);
Point(int, float, float);
float GetX();
void SetX(float);
float GetY();
void SetY(float);
int GetId();
void SetId(int);bool isBetweenTwoPoints(Point, Point); /*ovverride*/ //string toString(); //override bool Equals(object); static float calculateEuclideanDist(Point, Point);
private:
float x;
float y;
int Id;};
#endif // POINT_H
@@#ifndef POINTCOLLECTION_H
#define POINTCOLLECTION_H#include "Point.h"
#include <QVector>class PointCollection: public QVector<Point>
{
friend class Point;
public:
PointCollection();
Point getCenroid();
void setCentoird(Point);
void addPoint(Point);
Point deletePoint(int);
Point deletePoint(Point);
Point getNearestPointToCentroid();
Point PointT;
void updateCentroid();
private:
Point centorid;
float X;
float Y;
int Id;
};#endif // POINTCOLLECTION_H
@@#include "Point.h"
/**
- Default constructor
- @author Mikayel Egibyan
*/
Point::Point()
{
Id = -1;
x = -1;
y = -1;
}
/**
- Default constructor
- Returns the value of the x and y
- @author Mikayel Egibyan
- @param x, y The values of X and Y
*/
Point::Point(float x, float y)
{
this->Id = -1;
this->x = x;
this->y = y;
}
/**
- Default constructor
- Returns the value of the x and y
- @author Mikayel Egibyan
- @param x, y, Id The values of X,Y and Id
*/
Point::Point(int Id, float x, float y)
{
this->Id = Id;
this->x = x;
this->y = y;
}
/**
- This method is used to get a value of X
- @author Mikayel Egibyan
- @return The value of the X
*/
float Point::GetX()
{
return x;
}
/**
- This method is used to set a value X
- @author Mikayel Egibyan
- @param x X is the value of x
*/
void Point::SetX(float x)
{
this->x = x;
}
/**
- This method is used to get a value of Y
- @author Mikayel Egibyan
- @return The value of the Y
*/
float Point::GetY()
{
return y;
}
/**
- This method is used to set a value Y
- @author Mikayel Egibyan
- @param y Y is the value of y
*/
void Point::SetY(float y)
{
this->y = y;
}
/**
- This method is used to get a value of Id
- @author Mikayel Egibyan
- @return The value of the Id
*/
int Point::GetId()
{
return Id;
}
/**
- This method is used to set a value Id
- @author Mikayel Egibyan
- @param Id Id is the value of Id
*/
void Point::SetId(int Id)
{
this->Id = Id;
}
/**
- This method is used to find out if the values are between to gieven points
- @author Mikayel Egibyan
*/
bool Point::isBetweenTwoPoints(Point point_1, Point point_2)
{
if(this->x >= point_1.x && this->x <= point_2.x)
{
if(this->y >= point_1.y && this->y <= point_2.y)
{
return true;
}
}
}
/**
-
This method is used to get the Euclidean distance between two points
-
@author Mikayel Egibyan
-
@param point_1,point_2 point_1,point_2 are Point type objects
*/
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);
}
/*
bool Point::Equals(object obj)
{
if (obj is Point)
{
Point point = (Point)obj;
if (this.X == point.X && this.Y == point.Y)
{
return (true);
}
}return (false);
}
*//*
string Point::toString()
{
return ("{" + this->x + "," + this->y + "}");
}
*/
@@#include "PointCollection.h"
/**
- Default constructor
- @author Mikayel Egibyan
*/
PointCollection::PointCollection()
{
//centorid = new Point;
Id = PointT.GetId();
X = PointT.GetX();
Y = PointT.GetY();
}
/**
- This method is used to get a value of centroid
- @author Mikayel Egibyan
- @return The value of the centroid
*/
Point PointCollection::getCenroid()
{
return centorid;
}
/**
- This method is used to set a value centroid
- @author Mikayel Egibyan
- @param centroid Centroid is Point type object
*/
void PointCollection::setCentoird(Point centroid)
{
this->centorid = centroid;
}
/**
- This method is used to update the centroid
- @author Mikayel Egibyan
*/
void PointCollection::updateCentroid()
{
}
void PointCollection::addPoint(Point point_)
{
this->push_back(point_);
updateCentroid();
}Point PointCollection::deletePoint(int Index)
{
Point deleted = Point(this[Index].Id, this[Index].X, this[Index].Y);
this->remove(Index);
updateCentroid();
return(deleted);
}/*
Point PointCollection::deletePoint(Point point)
{
Point deleted = Point(point.GetId(), point.GetX(), point.GetY());
this->remove(point[]);
updateCentroid();
return(deleted);
}
*/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]);
}@