Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to use priority queue with custom classen. My class is node(name, .., .., distance, ...) but when in try to push a node into the queue i get a error.

node a = node("a",{}, 0, &a); node b = node("b",{}, INT_MAX, &a); node c = node("c",{}, INT_MAX, &a); node d = node("d", {}, INT_MAX, &a); node e = node("e", {}, INT_MAX, &a);
    
    edge a1 = edge(&a, &b,10); edge a2 = edge(&a, &c, 1); edge a3 = edge(&a, &d, 4);
    edge b1 = edge(&b, &d, 3); edge b2 = edge(&b, &e, 3);
    edge c1 = edge(&c, &d, 2);
    edge d1 = edge(&d, &e, 10); edge d2 = edge(&d, &b, 3);

    a.edges.push_back(&a1); a.edges.push_back(&a2); a.edges.push_back(&a3);
    b.edges.push_back(&b1); b.edges.push_back(&b2);
    c.edges.push_back(&c1);
    d.edges.push_back(&d1); d.edges.push_back(&d2);

    graph g = graph(
        {&a, &b, &c, &d, &e},
        {&a1,&a2,&a3,&b1,&b2,&c1,&d1,&d2}
    );

    node* start = &a;
    node* end = &e;


    std::vector<node*> eval = {start};
    std::vector<node*> test = {&a, &b, &c, &d};
    node* shortestNode;

    std::priority_queue<node> q;

    for(auto i : test)(
        q.push(i)
    )




What I have tried:

I have tried "copy paste" operator from the internet but none have been usefull.

The error i get is :
no instance of overloaded function "std::priority_queue<_Tp, _Sequence, _Compare>::push [with _Tp=node, _Sequence=std::vector<node, std::allocator<node>>, _Compare=std::less<node>]" matches the argument listC/C++(304)
main.cpp(37, 11): argument types are: (node *)
main.cpp(37, 11): object type is: std::priority_queue<node, std::vector<node, std::allocator<node>>, std::less<node>>
Posted
Updated 28-Jun-23 7:21am

Similar to your previous question, a priority_queue needs to know how to determine which object has the higher priority. You could add a custom comparator to the definition of the priority_queue, or you could add an operator<() method to your node class.
C++
// priority_queue with custom comparator
auto comp = [](const node& a, const node& b){return a.distanceToStart < b.distanceToStart;};
std::priority_queue<node, vector<node>, decltype(comp)> pq(comp);

// add an operator<() to node:
class node {
 // ...
 public:
    bool operator<()( const node& other) const { return distanceToStart < other.distanceToStart );
}
I prefer adding the operator<() method to the class, but that's not possible if you only have a pre-compiled class definition. You might also use a custom comparator if the priority for the queue is different that a "default sort order".
 
Share this answer
 
ObjectiveC
std::vector<node*> test = {&a, &b, &c, &d}; // test contains node pointers
node* shortestNode;

std::priority_queue<node> q; // priority_queue is declared to contain nodes rather than pointers to

for(auto i : test)(
    q.push(i) // trying to push a pointer into a container that wants an object
)
 
Share this answer
 
Another option is to implement a comparison method in your class specifically for the purpose of sorting. It has to be static since sort does not pass this pointer to it. You could also make a sort method that could also be static but necessarily. Those can look something like this :
C++
class node
{
public:
    using pnode  = node *;
    using vpnode = std::vector< pnode >;

    static bool CompareDistance( const pnode & lhs, const pnode & rhs )
    {
        return lhs->distanceToStart < rhs->distanceToStart;
    }

    static void SortNodes( vpnode & nodes )
    {
        std::sort( nodes.begin(), nodes.end(), CompareDistance );
    }
};
I find it helps clarity to have the using statements and a separate comparison method but opinions vary.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900