繁体   English   中英

C ++)无效的操作数到二进制表达式Error with Priority Queue

[英]C++ ) Invalid operands to binary expression Error with Priority Queue

我在另一个struct(B)上有一个struct(A)和Priority Queue(PQ)。

这是下面的结构A:

struct Node{
int level;
int total;
std::vector<int> sequence;

void clear(){
    sequence.clear();
}

void init(){
    level = 0;
    total = 0;
    sequence.clear();
}

long subjectNumber(){
    return sequence.size();
}

bool isInSequence(int index){
    for(int i = 0; i < sequence.size(); i++){
        if(index == sequence.at(i)){
            return true;
        }
    }
    return false;
}};

没什么特别的权利吗?

我使用如下所示的节点对象的优先级队列:

    std::priority_queue<Node> pq;

但是,当我运行项目时,出现错误:

无效的二进制二进制操作数(“常量节点”和“常量节点”)

我想把Node对象的总价值放在首位。如何解决这个问题?

UPDATED:
The picture is what I'm getting, at the project, there is no 'red'Line for me!

在此处输入图片说明

std::priority_queue要求元素类型提供重载的operator< (或通过Compare template参数的Compare器):

bool operator<(const Node& lhs, const Node &rhs) {
  // ...
}

为了能够使用std::priority_queue<Node> ,您需要为Node一个有效的小于运算符。

您可以将operator<重载定义为成员函数或非成员函数。

成员函数重载

struct Node{
   int level;
   int total;
   std::vector<int> sequence;

   void clear(){
      sequence.clear();
   }

   bool operator<(Node const& rhs) const { ... }
};

非成员函数重载

struct Node{
   int level;
   int total;
   std::vector<int> sequence;

   void clear(){
      sequence.clear();
   }

};

bool operator<(Node const& lhs, Node const& rhs) { ... }

使用Compare

您还可以使用Compare类,该类提供比较两个Node对象的功能:

struct NodeCompare
{
    bool operator()(Node const& lhs, Node const& rhs) { ... }
};

并使用它来构造std::priority_queue对象。

using MyQueue = std::priority_queue<Node, NodeCompare>;
MyQueue queue;

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM