简体   繁体   English

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

[英]error C2678: binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion)

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: /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; 在实例化'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]': _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: test.cpp:22:28:从这里要求/usr/include/c++/4.8/bits/stl_pair.h:215:51:错误:

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; 不匹配'operator =='(操作数类型是'const boost :: geometry :: model :: d2 :: point_xy'和'const boost :: geometry :: model :: d2 :: point_xy'){return __x。 first == __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;
}

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

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

Live example 实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C2678二进制&#39;==&#39;:找不到使用&#39;Card&#39;类型的左操作数的运算符(或者没有可接受的转换) - C2678 binary '==': no operator found which takes a left-hand operand of type 'Card' (or there is no acceptable conversion) 错误1错误C2678:二进制&#39;!=&#39;:未找到采用&#39;std :: ofstream&#39;类型的左操作数的运算符(或没有可接受的转换) - Error 1 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::ofstream' (or there is no acceptable conversion) 错误 C2678:二进制“=”:未找到采用“const std::string”类型的左操作数的运算符(或没有可接受的转换) - error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion) 错误C2678:二进制&#39;&gt;&gt;&#39;:未找到采用&#39;std :: istream&#39;类型的左操作数的运算符(或没有可接受的转换) - error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 错误C2678:二进制'=':找不到哪个运算符带有'const Recipe'类型的左手操作数(或者没有可接受的转换) - error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Recipe' (or there is no acceptable conversion) 错误C2678:二进制'<<':找不到运算符,它接受类型为'const std :: ofstream'的左手操作数(或者没有可接受的转换) - Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion) 错误C2678:二进制&#39;+&#39;:未找到采用&#39;volatile A&#39;类型的左操作数的运算符(或者没有可接受的转换) - error C2678: binary '+': no operator found which takes a left-hand operand of type 'volatile A' (or there is no acceptable conversion) 错误 C2678:找不到采用“const_Ty”类型的左侧操作数的运算符(或没有可接受的转换) - error C2678: no operator found which takes a left-hand operand of type 'const_Ty' (or there is no acceptable conversion) 错误C2678:二进制&#39;&gt;&gt;&#39;:未找到采用&#39;std :: stringstream类型的左操作数的运算符 - error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::stringstream 错误C2678:二进制&#39;==&#39;:未找到采用左侧操作数类型的运算符 - error C2678: binary '==': no operator found which takes a left-hand operand of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM