简体   繁体   English

Priority Queues C ++中的“没有匹配的成员函数可以调用”

[英]“No matching member function for call to push” in Priority Queues C++

I'm trying to implement a priority queue right now, but I keep getting an error saying that push is not a member function. 我现在正在尝试实现优先级队列,但是我一直收到错误消息,说push不是成员函数。 I never used priority queue before, so I'm pretty sure I'm messing up somewhere... 我以前从未使用过优先级队列,所以我很确定自己在某个地方搞砸了...

struct X
{
    unordered_map<string, double> distance;
    unordered_map<string, string> vertices;
};

void make(std::string source)
{
    X distance;
    X parent;
    priority_queue<X, std::vector<X>, greater<void>> pq; //i'm pretty sure there's something wrong with the templates?
    distance.distance[source] = 0;
    pq.push(std::make_pair(start, 0));

当您传递其他内容(一对)时, push期望(并接受) X类型的参数。

The class has to be comparable and must respond to operators like < and > so that the queue can sort them: 该类必须具有可比性,并且必须响应<和>之类的运算符,以便队列可以对它们进行排序:

class X {
    public:
    unordered_map<string, double> distance;
    unordered_map<string, string> vertices;
    int priority;
    bool operator< (const X& b) {
        return priority < b.priority;
    }
    bool operator> (const X& b) {
        return priority > b.priority;
    }
};

void make(std::string source) {
    priority_queue<X, vector<X>, greater<void>> pq;

    X element;
    element.distance[source] = 0;
    element.priority = 100;
    pq.push(element);
}

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

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