0

I have class Node as defined by:

class Node
{
   public:
      Node(string newName);
      Node();
      void setNodeName(string newName);
      string getNodeName();
      void attachNewNode(Node *newNode, int direction);
      Node *getAttachedNode(int direction);
   private:
      string name;
      Node *attachedNodes[4];
};

Node::Node(string newName)
{
   name = newName;
}

Node::Node()
{};

void Node::setNodeName(string newName)
{
   name = newName;
}

string Node::getNodeName()
{
   return name;
}

void Node::attachNewNode(Node *newNode, int direction)
{
   attachedNodes[direction] = newNode;
}

Node* Node::getAttachedNode(int direction)
{
   return attachedNodes[direction];
}

I have a file Maze1.txt:

9
A1
C3
A1 A2 B1 * *
A2 * B2 A1 *
A3 * B3 * *
B1 * * * A1
B2 B3 C2 * A2
B3 * * B2 A3
C1 C2 * * *
C2 C3 * C1 B2
C3 * * C2 *

Where 9 is the number of nodes to be created, A1 is the node we will begin navigation from, C3 is the node we will try to find a path to, and the following lines represent the nodes themselves and the pointers they have associated with them. For example:

A1 A2 B1 * *

represents node A1 has pointers to node A2 in the north, B1 in the east, null in the south, and null in the west.

A2 * B2 A1 *

represents node A2 has pointers to node null in the north, B2 in the east, A1 in the south, and null in the west.

I am trying to create a function that "builds" a "maze" of nodes. The following will set private variable Nodes startNode and endNode to their respective nodes and numNodes to the number of nodes as given by the file.

How can I process the string data to create Nodes for all the Node titles and then assign pointers where appropriate. Trying:

ifstream instream;
instream.open("Maze1.txt");
string line;
string data;
int numLines = 1;
int numNodes;
Node startNode();
Node endNode();

while(getline(instream, line))
{
   istringstream iss(line);
   data += line + "\n";
   iss.clear();

   if(numLines == 1)
   {
      istringstream buffer(line);
      buffer >> numNodes;
   }
   if(numLines == 2)
      Node startNode(line);
   if(numLines == 3)
      Node endNode(line);

   if(numLines > 3)
   {
      Node temp(line.substr(0,2));
      rooms.push_back(temp);
   }

   iss.clear();
   numLines++;
}

This will create and fill a vector of nodes each named the first node mentioned in each string line of the file. Following this loop, I need to run through another loop looking at each piece of the string and assign pointers to the appropriate Node in the vector. Trying:

ifstream repeat;
repeat.open(filename);
numLines = 1;
skipBlanks = 1;
int roomNum = 0;

while(getline(repeat, line))
{
   if(line.empty())
   {}
   else
   {
      istringstream iss(line);

      if(numLines == 1)
         skipBlanks++;
      if(numLines == 2)
         skipBlanks++;
      if(numLines == 3)
         skipBlanks++;

      if(numLines > 3 && skipBlanks > 3)
      {
         int first = line.find(" ", 0);
         int second = line.find(" ", first + 1);
         int third = line.find(" ", second + 1);
         int fourth = line.find(" ", third + 1);

         for(int i = 0; i < rooms.size(); i++)
         {
            if(rooms[i].getNodeName() == line.substr(first+1,2))
               rooms[roomNum].attachNewNode(&rooms[i],1);
            if(rooms[i].getNodeName() == line.substr(second+1,2))
               rooms[roomNum].attachNewNode(&rooms[i],2);
            if(rooms[i].getNodeName() == line.substr(third+1,2))
               rooms[roomNum].attachNewNode(&rooms[i],3);
            if(rooms[i].getNodeName() == line.substr(fourth+1,2))
               rooms[roomNum].attachNewNode(&rooms[i],4);
         }
      }

      roomNum++;
      numLines++;
      iss.clear();
   }
}

The program does in fact compile, but I am presented with "segmentation fault(core dumped)" which I can only guess comes from an out of bounds error? Can anyone help me with why this is happening? Is my program doing what it is supposed to aside from the fault and what should I do different to avoid this fault?

  • 2
    http://stackoverflow.com/questions/2346806/what-is-segmentation-fault – Piotr Siupa Nov 05 '15 at 16:37
  • sounds more like homework than an equal problem, try harder finding the solution –  Nov 05 '15 at 16:37
  • 1
    Try to debug the program and check in which line the segmentation fault occurs. – Piotr Siupa Nov 05 '15 at 16:38
  • 1
    Se also this list of common reasons for [segmentation fault](http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults), and [how to debug a segmentation fault with gdb](http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html). – Leiaz Nov 05 '15 at 16:49

0 Answers0