我有类Node定义:
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,南方的空白,西方的空白。
A2 * B2 A1 *
表示节点A2的指针指向北方的节点null,东方的B2,南方的A1,以及西方的null。
我正在尝试创建一个“构建”节点“迷宫”的功能。以下内容将私有变量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*’
这是什么意思?我怎样才能更正指针的分配?
答案 0 :(得分:0)
没有阅读完整的代码:错误说明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);
}
即。您必须在四次调用中添加&
,以便将指针传递给函数而不是对象本身。