我正在尝试使用优先级队列返回具有最低“计数”的对象“字节树”。因此,我在对象中实现了布尔运算符>函数。但这似乎不起作用,我得到基于其他东西的对象

我已经尝试将操作符

class Bytetree{
public:
bool leaf;
unsigned char byt;
int count;
Bytetree* child0;
Bytetree* child1;

    Bytetree(unsigned char c): byt(c), count(0), child0(nullptr), child1(nullptr), leaf(true){};
    Bytetree(Bytetree* c0, Bytetree* c1): child0(c0), child1(c1), count(c0->count+c1->count), leaf(false){};

         bool operator>(const Bytetree & right) {
    std::cout << "called at all" ;
    return count > right.count;
         }

[...]
 }


主要

...

 std::priority_queue<Bytetree*, std::deque<Bytetree*>, std::greater<Bytetree*> > que;
for (int i = 0; i<WORDLENGTH; i++){

    que.push(mywc.wordar[i]);
    //  mywc.wordar[i]->print();
}

while(que.size()>=2){
    Bytetree* bt0= que.top();
    que.pop();
    Bytetree* bt1= que.top();
    que.pop();

    que.push(new Bytetree(bt0, bt1));
}

最佳答案

您已使用std::greater<Bytetree *>,但已在operator >而不是Bytetree上声明了Bytetree *

07-27 13:51