Jump to content
  • Advertisement
Sign in to follow this  
Exxor

Please Help Stuck On What To Do Here (C++)

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

If you intended to correct an error in the post then please contact us.

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(): m_pHead(0) {}
	~Lobby() { Clear(); }
	Lobby(const lobby& c) // COPY CONSTRUCTOR I ADDED DONT KNOW IT THIS IS CORRECT	
	{	
          cout << "Copy constructor called";
		
	}
	
	void AddPlayer();
	void RemovePlayer();
	void Clear();
private:
	Player* m_pHead;
};
//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(): m_pHead(0) {}
	~Lobby() { Clear(); }
	Lobby(const lobby& c) // COPY CONSTRUCTOR I ADDED DONT KNOW IT THIS IS CORRECT	
	{	
          cout << "Copy constructor called";
		
	}
	
	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 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 = pNewPlayerl
	}
	//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_pHeadl
		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 << "\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 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;
}

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 this post


Link to post
Share on other sites
Advertisement

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:

 

VS2015 (Microsoft's Windows IDE): https://www.visualstudio.com/en-us/visual-studio-homepage-vs.aspx (click on "Download Community 2015")

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 this post


Link to post
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 this post


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!