简体   繁体   English

C ++优先对象队列在运行时错误为无效堆

[英]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.

相关问题 优先队列作为C ++中的堆 - Priority queue as heap in C++ 指针优先队列 C++ - Priority Queue of pointers C++ 使用指针和比较器C ++的优先级队列 - Priority queue with Pointers and Comparator C++ Static c++中指针的优先队列 - Static priority queue of pointers in c++ C ++)无效的操作数到二进制表达式Error with Priority Queue - C++ ) Invalid operands to binary expression Error with Priority Queue C ++标准模板库优先级队列抛出异常,消息“Invalid Heap” - C++ standard template library priority queue throws exception with message “Invalid Heap” C ++重载&gt;对于priority_queue(最小堆):在push()上为堆生成“对二进制表达式无效的操作数(&#39;const Node&#39;和&#39;const Node&#39;)” - C++ overloading > for priority_queue (min heap): yields “invalid operands to binary expression ('const Node' and 'const Node')” on push() for heap 使用自定义比较器(实现最小堆)实例化 int 对的 priority_queue 时 C++ 中的类型错误 - Type error in C++ when instantiating a priority_queue of int pairs with a custom comparator (to implement a min heap) C ++ STL优先级队列使用什么堆结构? - What heap structure does C++ STL priority queue use? A *优先级队列的C ++设置与向量+堆操作 - c++ set versus vector + heap operations for an A* priority queue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM