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();
}
}
However, I am given compilation errors for each of the attachNewNode(Node *newNode, int direction) function calls.
error: no matching function for call to ‘Node::attachNewNode(__gnu_cxx::__alloc_traits<std::allocator<Node> >::value_type&, int)’
rooms[roomNum].attachNewNode(rooms[i],1);
^
note: candidate is:
note: void Node::attachNewNode(Node*, int)
void Node::attachNewNode(Node *newNode, int direction)
^
note: no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<Node> >::value_type {aka Node}’ to ‘Node*’
What does this mean? And how can I correct my assigning of pointers?
Without reading your complete code: The error says, that Node::attachNewNode
expects a pointer to a Node
but you are giving him a Node
itself. This particular problem can be solved by changing your call from
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);
}
to
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);
}
I.e. you have to add a &
in your four calls, in order to pass a pointer to the function instead of the object itself.