Trouble going through an excerise

Started by
5 comments, last by AndrewBlalock 12 years, 8 months ago
I am currently going through the book C++ through game programming 2nd edition by Michael Dawson. I am on the 9th chapter and the final excerise and Ive looked through it and understand whats going on and how its doing it but I cant get it to compile. There is one error. I have tried making stuff public or having another function that returns m_pHead as you will see where it is. I believe the error is in the overloaded operator function. Here is that function by itself.

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


here is the whole code for the whole program to see it in its context.

// Game Lobby
// Stimulates 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(); }
void AddPlayer();
void RemovePlayer();
void Clear();
private:
Player *m_pHead;
};

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;
}
// else 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;
}

int main()
{
Lobby myLobby;
int choice;

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;

}


I understand what the code is doing like I said but I am curious why it wont compile. My understanding of the error is that it is not being allowed to access m_pHead at the beginning of the overloaded operator function beacuse it is private in the Lobby class. But since the function is declared a friend function in the Lobby class shouldnt it be allowed to access m_pHead. That is my understanding of friend functions.

Advertisement

There is one error.


Would you post what the error says?
[font="Consolas"][size="1"][font="Consolas"][size="1"]1>c:\users\andrew blalock\desktop\game lobby\main.cpp(109): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Lobby' (or there is no acceptable conversion)

[/font][/font]
You need to pass ostream by reference to operator<<
changeostream& operator<<(ostream os, const Lobby& aLobby) to ostream& operator<<(ostream& os, const Lobby& aLobby)

You also try to access Lobby::m_pHead but you can't because it's private. You will have to make m_pHead public or declare operator<< as a friend of Lobby. friend ostream& operator<<(ostream&, const Lobby&);
I changed the reference and also make Player *m_pHead in the Lobby class public but since the function is a friend in the Lobby class I thought that let you access the variable even though its private.
You are right. If it's a friend you should not have to make m_pHead public.
ok I figured it out I had commented the function decleration in the Lobby class when I was trying to figure out what was wrong so I uncommented it and made m_pHead private and it worked find. Thanks

This topic is closed to new replies.

Advertisement