错误C2678:二进制'==':找不到哪个运算符采用类型的左操作数(或者没有可接受的转换)

And*_*lov 10 c++ templates operator-overloading boost-geometry

我正在尝试编译以下代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

VS2012 C++编译器返回以下编译错误:

... VC\include\utility(219):错误C2678:二进制'==':找不到哪个运算符带有'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'){return __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;
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*ica 12

这种失败的原因是operator ==for std::pair用于==比较对的成员,后者又使用依赖参数的查找(ADL)来找到operator ==它们的适当对象.但是你已经在错误的命名空间中提供了重载,因为Point实际上是一个typedef ::boost::geometry::model::d2,而不是in ::.

如果您将运算符移动到正确的命名空间(无论如何这是一个好主意),它的工作原理如下:

#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;

namespace boost { namespace geometry { namespace model { namespace d2 {

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;
}
Run Code Online (Sandbox Code Playgroud)

实例

  • 关于提出的解决方案,我不太喜欢将代码添加到您没有*拥有*的命名空间.而不是添加运算符,我会试图找出图书馆提出的替代方案(我不希望这是对图书馆的疏忽,但故意因为某些原因而抛弃我而离开).通过向其库中添加代码,您可能会破坏下一次更新(如果将来添加运算符会怎么样?)和未定义的行为(如果两个翻译单元看到/看不到运算符,或者看到不同的版本会怎么样?)我会创建一个命名函数. (3认同)