有关创建新对象的运行时错误

时间:2013-01-10 17:35:45

标签: c++ runtime

我正在尝试编写一个简单的游戏大厅程序(没有实际的网络,只是模拟。)我在测试程序时遇到了运行时错误,我不知道如何修复它。 (我对编程很新。)

我的错误是,当我将新玩家节点转入大厅时,程序会接受我输入的前两个玩家名称,但在第三次尝试时我输入一个新名称,然后按回车但光标转到换行而不是进入新节点。我怀疑问题出在Lobby :: Add函数的某处,但我不知道在哪里。任何帮助或想法将不胜感激。谢谢=)。

#include <iostream>
#include <string>

using namespace std;

class Player
{
public:

    Player(const string& name = "");
    string GetName() const;
    Player* GetNext() const;
    void SetNext(Player* next);

private:
    string m_Name;
    Player* m_pNext;
};

Player::Player(const string& name):
    m_Name(name),
    m_pNext(0)
{}

string Player::GetName() const
{
    return m_Name;
}

Player* Player::GetNext() const
{
    return m_pNext;
}

void Player::SetNext(Player* next)
{
    m_pNext = next;
}

class Lobby
{
    friend ostream& operator<<(ostream& os, const Lobby& aLobby);

public:
    Lobby();
    ~Lobby();
    void Add();
    void Remove();
    void Clear();

private:
    Player* m_pHead;
};

Lobby::Lobby():
m_pHead(0)
{}

Lobby::~Lobby()
{
    Clear();
}

void Lobby::Add()
{
    // Create a new player node
    cout << "Please enter the name of new player: ";
    string name;
    cin >> name;
    Player* pNewPlayer = new Player(name);

    //If list is empty make head of list this new player
    if (m_pHead == 0)
    {
        m_pHead = pNewPlayer;
    } 

    else
    {
        Player* pIter = m_pHead;

        while(pIter->GetNext() != 0)
        {
            pIter->GetNext();
        }

        pIter->SetNext(pNewPlayer);
    }
}

void Lobby::Remove()
{
    if(m_pHead == 0)
    {
        cout << "The game lobby is empty, there are no players to remove!\n\n";
    }

    else
    {
        Player* pTemp = m_pHead;
        m_pHead = m_pHead->GetNext();
        delete pTemp;
    }
}

void Lobby::Clear()
{
    while(m_pHead != 0)
    {
        Remove();
    }
}

ostream& operator<<(ostream& os, const Lobby& aLobby)
{
    Player* pIter = aLobby.m_pHead;

    cout << "Here's who is in the game lobby: \n";

    if (pIter == 0)
    {
        cout << "The lobby is empty.\n";
    }

    else
    {
        while(pIter != 0)
        {
            os << pIter->GetName() << endl;
            pIter = pIter->GetNext();
        }
    }

    return os;
}

int main()
{
    Lobby myLobby;
    int choice;

    do
    {
        cout << myLobby;
        cout << "\nWelcome to the game lobby!\n";
        cout << "Please enter a choice.\n";
        cout << "0 - Quit the program.\n";
        cout << "1 - Add a player to the lobby.\n";
        cout << "2 - Remove a player from the lobby.\n";
        cout << "3 - Clear the lobby.\n\n";

        cout << "Choice: ";
        cin >> choice;

        switch(choice)
        {
            case 0: cout << "Goodbye!"; break;
            case 1: myLobby.Add(); break;
            case 2: myLobby.Remove(); break;
            case 3: myLobby.Clear(); break;
            default: cout << "Please enter a valid choice.\n"; break;
        }

    }while(choice != 0);

    return 0;
}

1 个答案:

答案 0 :(得分:1)

添加时会出现无限循环。您致电GetNext()但忘记将结果分配给pIter

我倾向于同意别人说的话,你需要学习如何使用调试器。它还有助于在发布之前将有问题的代码减少到最小的代码段。在许多情况下,这已经允许您自己找到问题。在您的代码中,您应该使用固定值替换std::cin的输入,这样人们就不必猜测他们需要输入的内容。

祝你好运!

乌利