运算符==错误
Operator == error
我定义了一个类Point。我还有一个类PointCollection:class PointCollection: public QVector<Point>
在实现一些方法时,我会得到以下错误:
错误:"operator=="不匹配(操作数类型为"Point"answers"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);
}
问题是,为了实现indexOf,QVector必须知道如何比较相等的点(否则如何在向量中找到点)。它使用了运算符==,但您还没有为类Point编写运算符==。因此会出现此错误。只需为Point写运算符==(和运算符!=也是个好主意)。
bool operator==(const Point& x, const Point& y)
{
// your code here
}
bool operator!=(const Point& x, const Point& y)
{
return !(x == y);
}
相关文章:
- 在运算符重载定义中使用成员函数(const错误)
- 逻辑运算符上出现错误代码 a')'
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++:需要帮助了解运算符重载错误
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- C++分数混合比较运算符错误
- 为什么我收到错误:"运算符<<不匹配?
- 迭代器类的重载前缀增量运算符会引发分段错误
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- 调用运算符时出现链接器错误<<,如何解决?
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 解决错误:运算符 [] 不匹配
- 提升错误:"运算符=="的重载不明确
- 字符串输入中没有匹配的错误运算符错误
- 错误:运算符 = IN C++ 不匹配
- QT 创建器错误(运算符 + 不匹配)
- 错误C++运算符重载
- 错误:运算符==不匹配
- C++ 语法和编译器错误 - 运算符不匹配<<
- C++错误--运算符=不匹配