I have a node class that has a Node* parentNode atribute.
My constructor is as follows:
Node::Node(Node* p,const State& s, int d) : parentNode(p), currentState(s), depth(d){ }
BUT with this constructor I get a problem:
I'm using this class to run a AI Search and at depth 3 it generates nodes with
parentNode == this
then when I try to get the PATH to that node the program enters a infinite loop.
if I use a constructor such as this:
Node::Node(Node* p,const State& s, int d) : parentNode(0), currentState(s), depth(d)
{
if(p != NULL)
parentNode = new Node(p->parentNode, p->currentState, p->depth);
}
it doesn't happen, the program works perfectly.
The method that Expands each node is this one:
std::vector<Node> Node::Expand()
{
std::vector<Node> children; //vector to be returned
//the function called here return a vector of new states
std::vector<State> possibleStates = currentState.NextPossibleStates();
for(int i = 0 ; i < (int)possibleStates.size(); i++)
{
//here's the point at which I create new nodes passing this as parent
Node n(this, possibleStates[i], depth + 1);
children.push_back(n);
}
return children;
}
The function that gets the path to a node is this one
std::vector<State> Node::Path()
{
std::vector<State> path;
Node* currentNode = (this);
while(currentNode != NULL)
{
path.push_back(currentNode->currentState);
currentNode = currentNode->parentNode;
}
std::reverse(path.begin(), path.end()); //reverses the path so it's in the right order
return path;
}
Any guess on what could be happening ? (If anyone needs I can just send all the headers and .cpp files