简体   繁体   English

将元素推入优先级队列时,二进制表达式的操作数无效

[英]invalid operands to binary expression when push element in a priority queue

I'm new in c++ programming.我是 c++ 编程的新手。 I've to create a priority queue of a Class named Container.我必须创建一个名为 Container 的 Class 的优先级队列。 So I've written this way:所以我这样写:

In main.cpp:在 main.cpp 中:

struct Comp{
    bool operator() (const Container& a, const Container& b){
        return a<b;
    }
};

std::priority_queue<Container, std::list<Container>, Comp> containers;

In Container.cpp:在 Container.cpp 中:

class Container{
public:
    friend bool operator< (const Container& a, const Container& b);
    //...
};

bool operator<(const Container &a, const Container &b) {
    return a.y<b.y;
}

I don't know why, even if I've declared a < operator overload, it gives me the same error:我不知道为什么,即使我声明了 < 运算符重载,它也会给我同样的错误:

error: invalid operands to binary expression ('std::__1::__list_iterator<Container, void *>' and 'std::__1::__list_iterator<Container, void *>')
    __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first); 

How can I resolve this?我该如何解决这个问题? I don't really know what's going on:(我真的不知道发生了什么:(

It's telling you that your it can't calculate the difference between two iterators of type list<Container>::interator .它告诉您它无法计算list<Container>::interator类型的两个迭代器之间的差异。

If you look at the requirements for priority_queue on CppReference it says that the iterators for the container "must satisfy the requirements of LegacyRandomAccessIterator."如果您查看CppReference上对priority_queue的要求,它会说容器的迭代器“必须满足 LegacyRandomAccessIterator 的要求”。

list s iterators are not random-access; list的迭代器不是随机访问的; they are bidirectional.它们是双向的。 Therefore, you can't use a list as the underlying container for a priority_queue .因此,您不能将list用作priority_queue的底层容器。

Simple fix: Use a deque instead of a list .简单修复:使用deque而不是list

std::priority_queue<Container, std::deque<Container>, Comp> containers;

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

相关问题 C ++)无效的操作数到二进制表达式Error with Priority Queue - C++ ) Invalid operands to binary expression Error with Priority Queue 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 将对象放入 C++ 中的优先级队列导致二进制表达式的无效操作数 - Putting objects into Priority Queue in C++ causing invalid operands to binary expression 二进制表达式的无效操作数? - invalid operands to binary expression? 重载运算符时对二进制表达式无效的操作数 - invalid operands to binary expression when overloading a operator 无效的二进制操作数 - Invalid operands to binary expression 将节点添加到pirority队列(无效的操作数到二进制表达式) - adding nodes to pirority queue(invalid operands to binary expression) 错误:二进制表达式的操作数无效 - error: invalid operands to binary expression TGridOptions上二进制表达式的无效操作数 - Invalid operands to binary expression on TGridOptions “二进制表达式的无效操作数”错误? - “invalid operands to binary expression” error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM