Sign in to follow this  
agm_ultimatex

trouble with game lobby exercise beginning game programming

Recommended Posts

Im getting an access violation when the program boots, and im unsure how to fix, please help gurus. The line return *m_pHead; is what causes it to fail when the new player(); is called. Code:
//Game Lobby
//Simulates a game lobby where players wait

#include <iostream>
#include <string>

using namespace std;

class Player
{
public:  
    Player(const string& name = ""): m_Name(name), m_pNext(0) {}
    string GetName() const { return m_Name; }
    Player* GetNext() const { return m_pNext; }
    void SetNext(Player* next) { m_pNext = next; }
private:
    string m_Name;
    Player* m_pNext;  //Pointer to next player in list
};

class Lobby
{
    friend ostream& operator<<(ostream& os, const Lobby& aLobby);
	
public:
    Lobby(): m_pHead(0) {}
    ~Lobby() { Clear(); }
	Lobby(const Lobby& l)
	{
		cout << "Copy constructor called" << std::endl;
		m_pHead = new Player();
		*m_pHead = l.GetMPHead();
	}
	Player& operator=(const Lobby& l)
	{
		std::cout << "Overloading operator" << std::endl;
		if(this->m_pHead == l.m_pHead)
		{
			return *m_pHead;
		}
		else
		{
			*m_pHead = l.GetMPHead();
			return this->GetMPHead();
		}
	}
    void AddPlayer();
	void Greeting();
    void RemovePlayer();
    void Clear();
	Player GetMPHead() const
	{
		return *m_pHead;
	}
private:
    Player* m_pHead;  
};

void Lobby::Greeting()
{
	cout << "Welcome to the lobby" << endl;
}

void Lobby::AddPlayer()
{
    //create a new player node
    cout << "Please enter the name of the 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;
    }
    //otherwise find the end of the list and add the player there
    else
    {
        Player* pIter = m_pHead;
        while (pIter->GetNext() != 0)
        {
            pIter = pIter->GetNext();       
        }
        pIter->SetNext(pNewPlayer);
    }
}

void Lobby::RemovePlayer()
{
    if (m_pHead == 0)
    {
        cout << "The game lobby is empty.  No one to remove!\n";
    }
    else
    {
        Player* pTemp = m_pHead;
        m_pHead = m_pHead->GetNext();
        delete pTemp;
    }
}

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

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

    os << "\nHere's who's in the game lobby:\n";
    if (pIter == 0)
    {
        os << "The lobby is empty.\n";
    }
    else
    {
        while (pIter != 0)
        {   
            os << pIter->GetName() << endl;
	        pIter = pIter->GetNext();
        }
    }

    return os;
}

void testCopyConstructor(Lobby lob)
{
	lob.AddPlayer();
}

int main()
{
    Lobby myLobby;
    int choice;
    testCopyConstructor(myLobby);
    do
	{
	    cout << myLobby;
        cout << "\nGAME LOBBY\n";
        cout << "0 - Exit 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";
        cout << endl << "Enter choice: ";
        cin >> choice;

        switch (choice)
        {
            case 0: cout << "Good-bye.\n"; break;
	        case 1: myLobby.AddPlayer(); break;  
            case 2: myLobby.RemovePlayer(); break;
            case 3: myLobby.Clear(); break;
            default: cout << "That was not a valid choice.\n";
        }
	}
    while (choice != 0);
    
    return 0;
}


Please help

Share this post


Link to post
Share on other sites
No player object has been initialized yet, and it worked fine before i started building the copy constructor.


[s]Woot, just got it. Your guess led me to victory. It was because i did not allocate memory for the m_pHead in the constructor. do'h.[/s]

I actually realized that won't work, as it will leave that new player behind as a leak when ever m_pHead is set to the first player added to the lobby. So i have now:


Lobby(): m_pHead(0), m_pTail(0)
{

}

~Lobby() { Clear(); }
Lobby(const Lobby& l): m_pHead(0), m_pTail(0)
{
cout << "Copy Constructor" << endl;
}




[Edited by - agm_ultimatex on April 23, 2008 6:31:17 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this