c++ - 比较双向链表中的指针?

标签 c++ pointers linked-list equality

我正在尝试为决赛周构建一个简单的文字冒险。 这是非常标准的东西。使用“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();
        }
    }
//...
}

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

最佳答案

这段代码

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 中,我认为您可以通过更改行来解决此问题

availableDoors[i] = allDoors[i]

availableDoors[availableDoorsLen] = allDoors[i]

关于c++ - 比较双向链表中的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16330539/

相关文章:

c - 在 C 中释放 char[] 会出现错误,C 编译器如何处理该 char[]

c - 通过引用传递结构并对其进行操作

c - 访问链表节点中的位数组

C# PInvoke 传递无符号字符 *

c++ - 如何创建库?

c++ - 禁用主窗口的标题上下文菜单

c++ - 使用 json c++ 的输出在字段中获取奇怪的字符

java - 按字母顺序对名称链接列表进行排序

c - 如何仅使用一个参数重写我的removeString 函数?

c++ - Cocos2d/Cocos2d-x粒子系统暂停