我试图编译下面的代码: 错误C2678:二进制“==”:没有操作员发现这需要类型的左边的操作数(或没有可接受的转化率)
#include <boost/geometry/geometries/point_xy.hpp>
#include <iostream>
#include <utility>
typedef boost::geometry::model::d2::point_xy<long> Point;
typedef std::pair<Point, Point> Vector;
bool operator==(const Point& p1, const Point& p2) {
return p1.x() == p2.x() && p1.y() == p2.y();
}
int main() {
Vector vec1(Point(0,0), Point(1,1));
Vector vec2(Point(0,0), Point(1,2));
std::cout << ((vec1 == vec2) == false) << std::endl;
std::cout << ((vec1 == vec1) == true) << std::endl;
}
VS2012 C++编译器返回以下编译错误:
...VC\include\utility(219): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Point' (or there is no acceptable conversion)
GCC C++编译器返回以下编译错误:
/usr/include/c++/4.8/bits/stl_pair.h:
In instantiation of ‘bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = boost::geometry::model::d2::point_xy; _T2 = boost::geometry::model::d2::point_xy]’:
test.cpp:22:28: required from here /usr/include/c++/4.8/bits/stl_pair.h:215:51: error:
no match for ‘operator==’ (operand types are ‘const boost::geometry::model::d2::point_xy’ and ‘const boost::geometry::model::d2::point_xy’) { return __x.first == __y.first && __x.second == __y.second; }
错误消失,如果我重载==操作符的矢量:
bool operator==(const Vector& v1, const Vector& v2) {
return v1.first == v2.first && v1.second == v2.second;
}
@WhozCraig是的,因为''==为'标准定义:: pair'由标准库。 – Angew 2014-10-09 08:28:57
@WhozCraig:如果比较'Point'对象的运算符已经被ADL找到,那么就不需要为'Vector'提供'operator =='。 – 2014-10-09 08:46:57