Reputation: 197
I have been trying to implement a priority queue in c++ for about 5 hours now.
I dont believe my comparator functor is doing what it should be but for the life of me I can't work out why.
At the bottom of my Node class I have a struct CompareNode, the Node class has a function to return an int member variable.
class Node
{
public:
Node();
~Node();
Node(const Node &obj);
int GetX() const { return x; }
int GetY() const { return y; }
int GetG() const { return g; }
int GetH() const { return h; }
int GetF() const { return f; }
int GetTerrainPenalty() const { return terrainPenalty; }
bool GetOpenList() const { return openList; }
bool GetClosedList() const { return closedList; }
Node* GetParentNode() const { return parentNode; }
std::vector<Node*> const GetNeighbours() { return neighbours; }
void SetX(int x) { this->x = x; }
void SetY(int y) { this->y = y; }
void SetG(int g) { this->g = g; }
void SetH(int h) { this->h = h; }
void SetF(int f) { this->f = f; }
void SetTerrainPenalty(int t) { this->terrainPenalty = t; }
void SetOpenList(bool b) { this->openList = b; }
void SetClosedList(bool b) { this->closedList = b; }
void SetParentNode(Node* n) { this->parentNode = n; }
void SetNeighbours(std::vector<Node*> n) { this->neighbours = n; }
void AddNeighbour(Node* n) { neighbours.push_back(n); }
// Manahattan Distance
void CalculateH(Node* end);
void CalculateF();
private:
int x;
int y;
int g;
int h;
int f;
int terrainPenalty;
bool openList;
bool closedList;
Node* parentNode;
std::vector<Node*> neighbours;
};
struct CompareNode
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return lhs->GetF() < rhs->GetF();
}
};
In my main.cpp I declare the priority queue.
std::priority_queue<Node*, std::vector<Node*>, CompareNode> openList;
I get a Debug Assertion Failed error, Invalid Heap.
When debugging it seems that when I call openList.top() it doesn't return the correct Node.
Any ideas what I am doing wrong?
Upvotes: 0
Views: 1780
Reputation: 96241
My psychic debugging powers tell me that since you didn't implement a copy constructor for your Node
, that you're accidentally copying it somewhere resulting in a double delete.
Upvotes: 1