priority_queue of objects

hello everybody.
i don't exactly understand why this code does not work. i'm simply trying to create a priority_queue of objects from a class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <queue>

class Node{
public:
    int x, y;
    int f, g, h;
    Node* parent = nullptr;

    //constructor
    Node(int x, int y){
        this->x = x;
        this->y = y;
    }
    //overload of <
    bool operator < (Node& n){
        if (f < n.f){
            return true;
        }
        else{
            return false;
        }
    }
};

int main(){
    Node start_node(10, 10);
    std::priority_queue<Node> open_nodes;
    open_nodes.push(start_node);

    return 0;
}


the error i get is too big to be posted here, as the maximum allowed are 9000 chars, but it can be seen here: http://cpp.sh/2osh4
i'm trying to implement A* pathfinding algorithm.
thanks in advance!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <queue>

class Node{
public:
    int x, y;
    int f, g, h;
    Node* parent = nullptr;

    //constructor
    Node(int x, int y){
        this->x = x;
        this->y = y;
    }
    //overload of <
    bool operator < (const Node& n) const
    {
        if (f < n.f){
            return true;
        }
        else{
            return false;
        }
    }
};

int main(){
    Node start_node(10, 10);
    std::priority_queue<Node> open_nodes;
    open_nodes.push(start_node);

    return 0;
}
Last edited on
thank you, i searched why it should be const, and now i understand :)
Topic archived. No new replies allowed.