[英]C++ Priority queue of object pointers at runtime error as invalid heap
我一直在嘗試在C ++中實現優先級隊列大約5個小時。
我不相信我的比較函子正在做應該做的事情,但是對於我一生,我無法弄清楚為什么。
在我的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();
}
};
在main.cpp中,我聲明優先級隊列。
std::priority_queue<Node*, std::vector<Node*>, CompareNode> openList;
我收到調試斷言失敗錯誤,無效堆。
調試時,似乎當我調用openList.top()時,它沒有返回正確的Node。
有什么想法我做錯了嗎?
我靈敏的調試能力告訴我,由於您沒有為Node
實現副本構造函數,因此您不小心將其復制到某個地方,從而導致兩次刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.