我正在努力为决赛周建立一个简单的文字冒险。 这是非常标准的东西。用'n','e','s'和'w'穿过房子,试着走到迷宫尽头。 一切都进行得很顺利,但是当我尝试检索可用门列表时,我遇到了一个问题。
这是我的基本设置
class Node
{
public:
//...
Node* getNLink() {return northLink;}
Node* getELink() {return eastLink;}
Node* getSLink() {return southLink;}
Node* getWLink() {return westLink;}
//...
void printAllPossibleMoves();
//checks all four links and tells the user which ones are not set to NULL
private:
//...
Node* northLink;
Node* eastLink;
Node* southLink;
Node* westLink;
const string dirNodeToStr(Node* dirNode);
//Takes a node pointer and returns whether that link is n/e/s/w, no spaces
};
我已经删除了所有多余的成员。 我的问题来自Node类中的两个成员函数。 首先,printAllPossibleMoves()获取所有未设置为NULL的指针的列表,并将这些指针逐个提供给dirNodeToStr()
void Node::printAllPossibleMoves()
{
Node* allDoors[4] = {getNLink(), getELink(), getSLink(), getWLink()};
//gets a list of all four pointers
Node* availableDoors[4];
int allDoorsLen(4), availableDoorsLen(0);
for(int i=0; i<allDoorsLen; i++)
{
if(allDoors[i] != NULL)
{
//filters out any NULL pointers and keeps track of the # of non-NULL pointers
availableDoors[i] = allDoors[i];
availableDoorsLen++;
}
}
if(availableDoorsLen == 0)
cout << "You don't see any doors in this room. Odd" << endl;
else if(availableDoorsLen == 1)
cout << "You see a door to the " << dirNodeToStr(availableDoors[0]) << endl; //CALL 1
else if(availableDoorsLen > 1 )
{
cout << "You see doors to the ";
for(int j=0; j<availableDoorsLen; j++)
{//make sure to put an 'and' in there before the last direction is printed
if(j == (availableDoorsLen-1))
cout << " and " << dirNodeToStr(availableDoors[j]) << endl; //CALL 2
else
cout << " " << dirNodeToStr(availableDoors[j]); //CALL 3
}
}
}
在三条标记的行上,printAllPossibleMoves()将一个Node指针传递给dirNodeToStr(),这是错误显现的地方。
const string Node::dirNodeToStr(Node* dirNode)
{
if(dirNode == dirNode->getNLink())
return "north";
else if(dirNode == dirNode->getELink())
return "east";
else if(dirNode == dirNode->getSLink())
return "south";
else if(dirNode == dirNode->getWLink())
return "west";
else
{
cout << "Error at Node::dirNodeToStr: Function was fed an invalid parameter" << endl;
//whenever this function is called, it falls through to this case
system("PAUSE");
exit(0);
}
}
输出:
This is the guest bedroom.
n
WEST HALL
This is a hallway.
You see doors to the Error at Node::dirNodeToStr: Function was fed an invalid pa
rameter
Press any key to continue . . .
如果重要,这是原始函数调用
void Node::movePlayer(Node*& pos, string direction)
{
if(direction == "north")
{
if(northLink == NULL)
cout << "You can't go north.\n";
else
{
pos = getNLink();
cout << pos->getRoomName() << endl << pos->getRoomInfo() << endl;
pos->printAllPossibleMoves();
}
}
//...
}
那你觉得怎么样?为什么指针不匹配?我收集了所有指针,将它们输入到另一个函数中,然后将其中一个指针与所有相同指针的列表进行比较。这不应该是一个明智的选择吗?
答案 0 :(得分:1)
此代码
for(int i=0; i<allDoorsLen; i++)
{
if(allDoors[i] != NULL)
{
//filters out any NULL pointers and keeps track of the # of non-NULL pointers
availableDoors[i] = allDoors[i];
availableDoorsLen++;
}
}
导致在您的可用门中放置NULL,我想您可以通过更改行来解决此问题
availableDoors[i] = allDoors[i]
要
availableDoors[availableDoorsLen] = allDoors[i]