c++对象指针的优先级队列在运行时错误为无效堆

C++ Priority queue of object pointers at runtime error as invalid heap

本文关键字:运行时错误 无效 队列 对象 指针 优先级 c++      更新时间:2023-10-16

我已经尝试在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实现复制构造函数,因此您不小心将其复制到某个地方导致双删除