简体   繁体   English

二进制表达式的无效操作数(常量点和常量点

[英]Invalid Operands to binary expression(const point and const point

    map<pair<int,int>,int>pairOfNumbers;
pairOfNumbers.insert(pair<pair<int,int>,int>({1,2},2));

this is working, but这是有效的,但是

    map<pair<point,point>,int>PointsOnLine;
PointsOnLine.insert(pair<pair<point,point>,int>(make_pair(points[i],points[j]),count));

this doesn't.这没有。

point is just a structure of two ints x and y; point 只是两个整数 x 和 y 的结构; I keep getting the error 'Invalid Operands to binary expression(const point and const point' this is the structure of point.我不断收到错误'二进制表达式的无效操作数(常量点和常量点'这是点的结构。

    struct point
{
    int x;
    int y;
public:
    bool operator==(const point& p)
    {
        if(x==p.x && y==p.y)
            return true;
        else
            return false;
    }
    bool operator!=(const point& p)
    {
        if(x==p.x &&y==p.y)
            return false;
        else
            return true;
    }
};

how do I insert two points and distance between them in the map?如何在 map 中插入两个点和它们之间的距离? in Xcode I get this error在 Xcode 我得到这个错误Xcode 中的错误信息

Your point type does not support weak-ordering.您的point类型不支持弱排序。 It has no method of determining is-less-than.它没有确定 is-less-than 的方法。 You may think you don't need that because your point is actually tucked into a std::pair<point,point> but you do.您可能认为您不需要它,因为您的point实际上隐藏在std::pair<point,point>但您确实需要。

std::pair<T1,T2> supports weak ordering only if T1 and T2 do. std::pair<T1,T2>仅在T1T2支持时才支持弱排序。 In your case, they're the same type, so for a std::pair<point,point> to be used as key in a std::map<std::pair<point,pint>,T> , point must support weak ordering.在您的情况下,它们是相同的类型,因此对于std::pair<point,point>用作std::map<std::pair<point,pint>,T>中的键, point必须支持弱排序。

To support weak ordering, you must either provide an operator< that compares two of your objects in question, or a comparator functor type that does the same thing.为了支持弱排序,您必须提供一个比较两个有问题的对象的operator< ,或者提供一个执行相同操作的比较器仿函数类型。 The easiest way for you to do this would be:您执行此操作的最简单方法是:

#include <tuple>

struct point
{
    int x;
    int y;

    bool operator <(const point& p) const
    {
        return std::tie(x, y) < std::tie(p.x, p.y);
    }

    bool operator ==(const point& p) const
    {
        return !(*this < p || p < *this);
    }

    bool operator !=(const point& p) const
    {
        return *this < p || p < *this;
    }
};

I took liberty to redefine operator == and operator != to utilize the weak order properties of the proper operator < .我冒昧地重新定义了operator ==operator !=以利用正确的operator <的弱顺序属性。 It wasn't necessary, but ultimately it's just easier if operators root to as basic code as possible.这不是必需的,但最终如果操作员根植到尽可能基本的代码会更容易。 With the above change you should be able to use both point and std::pair<point,point> as key types in std::map and std::set .通过上述更改,您应该能够同时使用pointstd::pair<point,point>作为std::mapstd::set中的键类型。 In truth, a strict weak ordering can define all of the basic comparators (<=, >, >=, ,=, ==) as derivations from operator < in one form or another (or relative to something that does).实际上,严格的弱排序可以将所有基本比较器(<=、>、>=、、=、==)定义为operator <以一种或另一种形式(或相对于某种形式)的派生。 I challenge you to consider them, try implementing them, and above all, writing some test harnesses that verify your implementation.我挑战你考虑它们,尝试实现它们,最重要的是,编写一些测试工具来验证你的实现。

暂无
暂无

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

相关问题 二进制表达式的无效操作数(&#39;RadioDevice&#39;和&#39;const RadioDevice&#39;) - Invalid operands to binary expression ('RadioDevice' and 'const RadioDevice') 无效的二进制二进制操作数(“ const Vector”和“ const Vector”) - Invalid operands to binary expression (‘const Vector’ and ‘const Vector’) 为什么添加“ const”可以解决“无效的操作数到二进制表达式” - Why does adding “const” solve “invalid operands to binary expression” vector::erase 失败,二进制表达式的操作数无效(T 和 const T) - vector::erase fails with invalid operands to binary expression (T and const T) 二进制表达式“`const Foo`和`const Foo`的操作数无效,没有匹配的排序调用 - Invalid operands to binary expression "`const Foo` and `const Foo`, no matching call to sort unordered_set:二进制表达式的无效操作数(“ const Play”和“ const Play”) - unordered_set: invalid operands to binary expression ('const Play' and 'const Play') 二进制“ operator +”类型为“ const char [8]”和“ const char *”类型的无效操作数 - Invalid operands of types ‘const char [8]’ and ‘const char*’ to binary ‘operator+ &#39;const char*&#39; 和 const char[4]&#39; 类型的无效操作数到二进制 &#39;operator+&#39; - invalid operands of types 'const char*' and const char[4]' to binary 'operator+' 'const char [18]' 和 'const char*' 类型的无效操作数到二进制 'operator - invalid operands of types ‘const char [18]’ and ‘const char*’ to binary ‘operator C ++重载&gt;对于priority_queue(最小堆):在push()上为堆生成“对二进制表达式无效的操作数(&#39;const Node&#39;和&#39;const Node&#39;)” - C++ overloading > for priority_queue (min heap): yields “invalid operands to binary expression ('const Node' and 'const Node')” on push() for heap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM