C++ 错误 C2678:二进制“==”:未找到采用左侧操作数类型的运算符(或没有可接受的转换)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26273570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2678: binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion)
提问by Andrey Dyatlov
I'm trying to compile the following code:
我正在尝试编译以下代码:
#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++ compiler returns the following compilation error:
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)
...VC\include\utility(219):错误 C2678:二进制“==”:未找到采用“const Point”类型左侧操作数的运算符(或没有可接受的转换)
GCC C++ compiler returns the following compilation error:
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; }
/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.第一个 == __y.first && __x.second == __y.second; }
Error disappears if I overload == operator for Vector:
如果我为 Vector 重载 == 运算符,错误就会消失:
bool operator==(const Vector& v1, const Vector& v2) {
return v1.first == v2.first && v1.second == v2.second;
}
回答by Angew is no longer proud of SO
The reason why this fails is that the operator ==
for std::pair
uses ==
to compare the pairs' members, which in turn uses argument-dependent lookup (ADL)to find the proper operator ==
for them. But you've provided the overload in the wrong namespace, since Point
is actually a typedef for something in ::boost::geometry::model::d2
, and not in ::
.
失败的原因是operator ==
forstd::pair
用于==
比较对的成员,而后者又使用依赖于参数的查找 (ADL)来找到适合operator ==
它们的成员。但您提供在错误的命名空间中的过载,因为Point
实际上是在东西的typedef ::boost::geometry::model::d2
,而不是在::
。
If you move the operator into the correct namespace (which is a good idea anyway), it works:
如果您将运算符移动到正确的命名空间(无论如何这是个好主意),它就可以工作:
#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;
}