[英]C++ Priority queue of object pointers at runtime error as invalid heap
I have been trying to implement a priority queue in c++ for about 5 hours now. 我一直在尝试在C ++中实现优先级队列大约5个小时。
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. 在我的Node类的底部,我有一个结构CompareNode,Node类具有一个返回int成员变量的函数。
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. 在main.cpp中,我声明优先级队列。
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. 调试时,似乎当我调用openList.top()时,它没有返回正确的Node。
Any ideas what I am doing wrong? 有什么想法我做错了吗?
我灵敏的调试能力告诉我,由于您没有为Node
实现副本构造函数,因此您不小心将其复制到某个地方,从而导致两次删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.