Hi, I am writing a program to solve the 8puzzle, but for some reason i am having more difficulty with the structure than the algorithm....

I have two classes, a tree and node class.
My main problem is to put the nodes in an array in the tree class.
Second, which is the one om having problems now, my "move" function in my Node class is giving me a seg fault if i create a int? it is also changing values of my root state?

Can someone please give me any idea where i am going wrong?

Thanks!

//inside my tree class
void Tree::addNode(int x)
{
    Node child;
    child = root.move(x);

//Here i actually want to add the child to my Node *nodes[] btut dont know how...



}

//inside my node class
Node Node::move(int nxt_blck)
{
    //x = nxt_blck;      //if i declare this it gives me a sg fault?!?
  Node *result;

  result->setState(state);

//the nxt_blck changes value here (to zero) and i dont know why...

  result->setParent(this);

  /* Swap hole and other piece. */

  result->state[zero] = state[nxt_blck];
  result->setZero(nxt_blck);
  result->state[nxt_blck] = state[zero];

  return *result;
}

//x = nxt_blck; //if i declare this it gives me a sg fault?!?

That's not where your segmentation fault is. It's on the next two lines:

Node *result; // Declaring uninitialized pointer
 
result->setState(state); // Using uninitialized pointer

A segmentation fault means that you're accessing memory outside of the process' address space. Most often this happens with pointers in one of three situations:

  • A null pointer
  • An uninitialized pointer
  • A dangling pointer

My main problem is to put the nodes in an array in the tree class.

I don't understand the problem. Please elaborate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.