Hey, I'm writing a UCS algorithm and came across an invalid heap error.
I have overloaded <= and < operators to work with the priority queue, but I have no idea what's wrong. I also debugged the code, and none of my containers is empty or have invalid or NULL data. The error happens after few iterations.
bool Manager::UCS()
{
priority_queue<Brick> myQueue;
myQueue.push(myBoard->getRoot());
mySet.insert(myBoard->getRoot().getCoorddinates());//I use the set to track the Bricks I have in my queue since there're no find function in queue.
while (!myQueue.empty())
{
Brick parent = myQueue.top();
myQueue.pop();
mySet.erase(parent.getCoorddinates());
if (parent.getSym() == 'G')
{
cout << parent.getTrackCostUpToMe();
returntrue;
}
stack<Brick> tempStack;
returnNeighbours(parent, &tempStack,myBoard->getNode(parent.getFatherX(),parent.getFatherY()));///Finds me all the neighbours of my brick and puts them in stack.
while (!tempStack.empty())
{
tempStack.top().setTrackCostUpToMe(parent.getTrackCostUpToMe());
if (myQueue.empty())
{
myQueue.push(tempStack.top());
mySet.insert(tempStack.top().getCoorddinates());
tempStack.pop();
}
else
{
if (mySet.find(tempStack.top().getCoorddinates()) == mySet.end())
{
myQueue.push(tempStack.top());/// Here's the line where I get the error
mySet.insert(tempStack.top().getCoorddinates());
tempStack.pop();
}
else tempStack.pop();
}
}
}
returnfalse;
}