I have a problem with pointers within a struct losing their data when added to a vector.
Within a pathfinder function, I'm creating a vector<Node>
and for each of those nodes I'm setting the Node.parent
to an address of Node.
After the nodes have been set with their parent, I pop these nodes into another vector<Node>
.
The problem I'm having is the nodes are losing their pointer to the parent after moving them into the new vector.
Essentially the code looks like this.
struct Node {
int x,y;
Node * parent;
}
vector<Node> queue;
queue.push_back(start)
while (queue.size() > 0) {
Node item = queue.back(); //grab a item from queue last thing put in
queue.pop_back(); //remove that item from the queue
traveled.push_back(item); //add item to list of traveled items.
std::vector<Node> neighbors = getNeighbors(item);
setParent(neighbors, &item);
//check each neighbor
for (std::vector<Node>::iterator iter = neighbors.begin(); iter != neighbors.end(); iter++) {
//check if a walkable Node and its not already traveled
if (validNode(*iter) && !contains(traveled, *iter)) {
queue.push_back(*iter);
}
}
}
Once I add the valid neighbor nodes back into the queue vector, they lose their pointer to the correct parent. They do retain the correct x,y values though.
Any help would be great!