【发布时间】:2014-12-04 02:20:15
【问题描述】:
我正在尝试编译以下代码:
#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 take a left-hand 'const Point' 类型的操作数(或没有可接受的转换)
GCC C++ 编译器返回以下编译错误:
/usr/include/c++/4.8/bits/stl_pair.h:
在 '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: 从这里需要 /usr/include/c++/4.8/bits/stl_pair.h:215:51: 错误:
“operator==”不匹配(操作数类型为“const boost::geometry::model::d2::point_xy”和“const boost::geometry::model::d2::point_xy”) { 返回 __x.first == __y.first && __x.second == __y.second; }
如果我为 Vector 重载 == 运算符,错误就会消失:
bool operator==(const Vector& v1, const Vector& v2) {
return v1.first == v2.first && v1.second == v2.second;
}
【问题讨论】:
-
@WhozCraig 是的,因为
==是由标准库为std::pair定义的。 -
@WhozCraig:如果 ADL 找到了比较
Point对象的运算符,则无需为Vectors 提供operator==。
标签: c++ templates operator-overloading boost-geometry