2013-05-02 97 views
0

我正在尝试构建一个简单的文本冒险总决赛周。 这是非常标准的东西。用'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()传递的一个指向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(); 
     } 
    } 
//... 
} 

所以,你有什么感想?为什么指针不匹配?我收集了所有的指针,将它们送入另一个函数,然后将其中的一个与所有相同指针的列表进行比较。这不应该是一个明智之举吗?

回答

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++; 
    } 
} 

导致空值放置在您的availableDoors,我想你可以通过改变线路

availableDoors[i] = allDoors[i] 

availableDoors[availableDoorsLen] = allDoors[i] 
+0

解决这个问题你肯定?在第3行,我一定要检查allDoors [i]是不是NULL – anthony 2013-05-02 05:07:47

+0

那么我会被诅咒。有用。那么我究竟是如何在我的列表中获得NULL? – anthony 2013-05-02 05:10:00

+0

哦,等一下。我现在明白了。如果我按照i来遍历availableDoors,如果我遇到门少于4个的房间,则列表不匹配。我现在明白了。谢谢你找我。 – anthony 2013-05-02 05:11:06