从字符串文件 c++ 创建一个节点迷宫
Create a maze of Nodes from string file c++
我有 class 个节点定义为:
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];
}
我有一个文件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 *
其中 9 是要创建的节点数,A1 是我们将开始导航的节点,C3 是我们将尝试找到路径的节点,以下几行代表节点本身和指针他们与他们有联系。例如:
A1 A2 B1 * *
表示节点A1有指针指向北边的节点A2,东边的B1,南边的null,西边的null。
A2 * B2 A1 *
表示节点A2在北有指向节点null的指针,在东有指向B2的指针,在南有指向A1的指针,在西有指向null的指针。
我正在尝试创建一个 "builds" 节点的 "maze" 函数。下面将私有变量 Nodes startNode 和 endNode 设置为它们各自的节点,并将 numNodes 设置为文件给定的节点数。
我如何处理字符串数据来为所有节点标题创建节点,然后在适当的地方分配指针。尝试:
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++;
}
这将创建并填充一个节点向量,每个节点命名为文件每个字符串行中提到的第一个节点。在这个循环之后,我需要 运行 通过另一个循环查看字符串的每一段并将指针分配给向量中的适当节点。正在尝试:
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();
}
}
但是,每次 attachNewNode(Node *newNode, int direction) 函数调用都出现编译错误。
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*’
这是什么意思?我怎样才能更正我的指针分配?
没有阅读你的完整代码:错误说,Node::attachNewNode
需要一个指向 Node
的指针,但你给了他一个 Node
本身。这个特殊问题可以通过改变你的电话来解决
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);
}
到
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);
}
即您必须在四个调用中添加一个 &
,以便将指针传递给函数而不是对象本身。
我有 class 个节点定义为:
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];
}
我有一个文件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 *
其中 9 是要创建的节点数,A1 是我们将开始导航的节点,C3 是我们将尝试找到路径的节点,以下几行代表节点本身和指针他们与他们有联系。例如:
A1 A2 B1 * *
表示节点A1有指针指向北边的节点A2,东边的B1,南边的null,西边的null。
A2 * B2 A1 *
表示节点A2在北有指向节点null的指针,在东有指向B2的指针,在南有指向A1的指针,在西有指向null的指针。
我正在尝试创建一个 "builds" 节点的 "maze" 函数。下面将私有变量 Nodes startNode 和 endNode 设置为它们各自的节点,并将 numNodes 设置为文件给定的节点数。
我如何处理字符串数据来为所有节点标题创建节点,然后在适当的地方分配指针。尝试:
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++;
}
这将创建并填充一个节点向量,每个节点命名为文件每个字符串行中提到的第一个节点。在这个循环之后,我需要 运行 通过另一个循环查看字符串的每一段并将指针分配给向量中的适当节点。正在尝试:
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();
}
}
但是,每次 attachNewNode(Node *newNode, int direction) 函数调用都出现编译错误。
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*’
这是什么意思?我怎样才能更正我的指针分配?
没有阅读你的完整代码:错误说,Node::attachNewNode
需要一个指向 Node
的指针,但你给了他一个 Node
本身。这个特殊问题可以通过改变你的电话来解决
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);
}
到
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);
}
即您必须在四个调用中添加一个 &
,以便将指针传递给函数而不是对象本身。