2013-01-10 46 views
0

我正在尝试编写一个简单的游戏大厅程序(没有实际的网络,只是一个模拟),而且我在测试程序时遇到运行时错误,我不知道如何去关于修理它。 (我对编程相当陌生)有关创建一个新对象的运行时错误

我的错误是当我去一个新的玩家节点进入大厅时,程序会接受我输入的前两个玩家名称,但在第三次尝试时我输入了一个新的名称,然后按回车键,但光标会变为新行,而不是进入新节点。我怀疑这个问题在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; 
} 
+2

这是一个非常多的代码。你能构建一个更简单的例子来证明同样的问题吗? –

+0

曾听说过一个调试器?另外,当提示用户输入某些内容时,你必须刷新输出。 – 2013-01-10 17:39:10

+1

你有没有考虑过使用'std :: list'或'std :: deque'来代替实现自己的链表? –

回答

1

添加时有一个无限循环。您致电GetNext(),但忘记将结果分配给pIter

我倾向于同意别人的看法,但您需要了解如何使用调试器。它还有助于在发布之前将有问题的代码减少到最小的一段代码。在很多情况下,这已经可以让你自己找到问题。在你的代码中,你应该用std::cin替换为固定值的输入,这样人们就不必猜测他们需要输入什么。

祝你好运!

Uli

+0

@Uli 谢谢,让它工作。我正在努力获得一些更好的硬件和更多的当前操作系统,希望我的调试器能够正常工作。此外,感谢您提供建议,我会在发布未来问题时记住它。 – Jammin