This topic is 878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there i am looking for some help with the exercises in my book for c++. The first ch9 exercise states: "Improve the lobby class from the Game Lobby program by writing a copy constructor and an overloaded assignment operator for it." I am confused on what i am doing here i have re read the chapter three times and can not make sense of it . i added these two lines to the lobby class but i dont know if this is correct or how to make it correct if it is not

class Lobby
{
friend ostream& operator<<(ostream& os, const Lobby& aLobby)
Lobby& operator=(const Lobby& c) //OVERLOADED ASSIGNMENT OPERATOR I ADDED DO NOT KNOW IF THIS WORKS

public:
~Lobby() { Clear(); }
Lobby(const lobby& c) // COPY CONSTRUCTOR I ADDED DONT KNOW IT THIS IS CORRECT
{
cout << "Copy constructor called";

}

void RemovePlayer();
void Clear();
private:
};
//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 = next; }
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)
Lobby& operator=(const Lobby& c) //OVERLOADED ASSIGNMENT OPERATOR I ADDED DO NOT KNOW IF THIS WORKS

public:
~Lobby() { Clear(); }
Lobby(const lobby& c) // COPY CONSTRUCTOR I ADDED DONT KNOW IT THIS IS CORRECT
{
cout << "Copy constructor called";

}

void RemovePlayer();
void Clear();
private:
};

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

//if list is empty, make head of list this new player
{
}
//otherwise find the end of the list and add the player there
else
{
while (pIter->GetNext() != 0)
{
pIter = pIter->GetNext();
}
pIter->SetNext(pNewPlayer);
}
}

void Lobby::RemovePlayer()
{
{
cout << "The game lobby is empty. No one to remove!\n";
}
else
{
delete pTemp;
}
}

void Lobby::Clear()
{
{
RemovePlayer();
}
}

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

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 << "\nGameLobby\n";
cout << "0 - Exit the programm.\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 2: myLobby.RemovePlayer(); break;
case 3: myLobby.Clear(); break;
default: cout << "That was not a valid choice.\n";
}
}
while (choice != 0);

return 0;
}

The second exercise states: " The lobby::AddPlayer() function is inefficient because it iterates through all of the player nodes to add a new player to the end of the line. Add an m_pTail pointer data member to the Lobby class that always points to the last node in the line and use it to more efficiently add a player." I have no idea how to add this so if anyone could please help that would be amazing thank you :).

Edited by Exxor

##### Share on other sites

This code will not compile. I see several spelling and syntax errors just at a casual glace. Before pursuing the exercise how about correcting the code that you have here and ensuring that your additions work correctly? (hint - the additions are not correct) For exercises like this you really ought to be working in some form of development environment where you're regularly compiling and checking your code. If you don't have one then here are some things you can use:

ideOne (online compiler - runs in browser): https://ideone.com/

Further, this seems like homework, especially since I can't imagine any other reason to reach this point without being able to at least venture a guess at how to implement this feature. If you understand how the linked list works (which is probably the objective of the section) then you really should have no difficulty with this whatsoever.

Edit: Yeah. I knew this code was familiar. This is from Beginning C++ Through Game Programming. Read through the section again (starting from "Introducing the Game Lobby Program") and get clear on how the system works. Once you've done that, remember that the container (the lobby) starts out empty and you add and remove elements (players) one at a time. You should be able to reason about how to keep a tail pointer up-to-date here. If not then you need to continue to review the program until you understand how it works.

I think it would be doing you a grave disservice to simply answer this question directly. Those questions exist to ensure that you understand the material.

Edited by Khatharr

##### Share on other sites

What IDE are you using? VS2015 is brilliant and it's free. It will help you spot errors so try that if it is an option (and you aren't already doing so of course). It'll help you spot places where you missed a capital letter, such as here:

Lobby(const lobby& c) // COPY CONSTRUCTOR I ADDED DONT KNOW IT THIS IS CORRECT
{
cout << "Copy constructor called";
}

Or just forgot a semiclor like here:

Lobby& operator=(const Lobby& c)

Some of these things can be difficult to track down, forgetting a semi colon from a header often takes me a moment or two to find. The declarations themselves are correct though (apart from the lower case l in the copy constructor), they are a copy constructor/assignment operator. One thing to note though is your assignment operator is private as is your << operator.

When working you should try to compile often (In Visual Studio you can press ctrl+F7 to compile just the file you are working on though that won't do anything in a .h file).

Edited by Nanoha

##### Share on other sites
Aside from the syntactical issue, the real exercise I believe is actually writing useful copy operations that fix bugs in the original source.

If you want a hit, double check the areas in your text that describe memory management and object lifetime (what is a pointer, what happens to an object that you create with new, what happens when you copy pointers, and then ask yourself what would happen if you made a copy of your Lobby object after adding players to it).

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013175
×