Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Static priority queue of pointers in c++

I have a class foo, and inside the class, i need a static priority queue bar that holds pointers to some number of foo objects, and the foo object also has a private member buzz that will hold the weight of the objects when compared.

So far, I have tried the following:

class foo{
private:
    // some stuff
    int buzz;
public:
    // some more stuffs
    static bool compare (const foo* l, const foo* r){
        return l->buzz < r->buzz;
    }
    static std::priority_queue<foo*, std::vector<foo*>, foo::compare> bar;
};

But I get this error in clang: template argument for template type parameter must be a type

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I read this and this but could not get my head around how to do it or what i was doing wrong.

>Solution :

As you can see in the std::priority_queue documentation,
the 3rd template argument Compare is:

A Compare type providing a strict weak ordering.

A function pointer (like you used) cannot be used for the compare type.
One way to supply a compare type is via a class with opertor():

#include <queue>

class foo {
private:
    int buzz;
public:
    // Our compare type:
    struct Compare
    {
        bool operator()(const foo& l, const foo& r) { return l.buzz < r.buzz; }
    };

    //--------------------------------------------------vvvvvvvvvvvv-----
    static std::priority_queue<foo*, std::vector<foo*>, foo::Compare> bar;
};

Note that the operator() accepts the arguments by const reference.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading