473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Not Using References Properly

hi,

my poker game does

playersActiveInRound = players;

at the start of every round where both are vector<Player> (i.e.,
containers of a user-defined class). the latter vector is everyone who
hasn't run out of money yet.

in a round, when someone folds I wish to remove them from
playersActiveInRound by calling

void Game::DropPlayer(Player& targetPlayer)
{

for(posPlayer = playersActiveInRound.begin(); posPlayer !=
playersActiveInRound.end(); ++posPlayer)
{
if((*posPlayer).GetFullName() == targetPlayer.GetFullName())
{
playersActiveInRound.erase(posPlayer);
}
}

}

and feeding it the Player identifier.

can anyone tell me why I get a crash everytime this function runs?

thx a lot

Nov 22 '05 #1
4 1239
<A_*********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
hi,

my poker game does

playersActiveInRound = players;

at the start of every round where both are vector<Player> (i.e.,
containers of a user-defined class). the latter vector is everyone who
hasn't run out of money yet.

in a round, when someone folds I wish to remove them from
playersActiveInRound by calling

void Game::DropPlayer(Player& targetPlayer)
{

for(posPlayer = playersActiveInRound.begin(); posPlayer !=
playersActiveInRound.end(); ++posPlayer)
{
if((*posPlayer).GetFullName() == targetPlayer.GetFullName())
{
playersActiveInRound.erase(posPlayer);
}
}

}

and feeding it the Player identifier.

can anyone tell me why I get a crash everytime this function runs?

thx a lot

The following is a fully functioning program that uses your method without
change. Your error must be somewhere else in code you haven't shown.

#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>
#include <windows.h>
#include <list>

class Player
{
public:
std::string GetFullName() { return FullName; };
void SetFullName( std::string Name ) { FullName = Name; };
private:
std::string FullName;
};

class Game
{
public:
void InsertPlayer( Player player ) { playersActiveInRound.push_back(
player ); };
void DropPlayer(Player& targetPlayer);
void ListPlayers();
private:
std::vector<Player> playersActiveInRound;
std::vector<Player>::iterator posPlayer;
};

void Game::DropPlayer(Player& targetPlayer)
{
for(posPlayer = playersActiveInRound.begin(); posPlayer !=
playersActiveInRound.end(); ++posPlayer)
{
if((*posPlayer).GetFullName() == targetPlayer.GetFullName())
{
playersActiveInRound.erase(posPlayer);
}
}
}

void Game::ListPlayers()
{
for(posPlayer = playersActiveInRound.begin(); posPlayer !=
playersActiveInRound.end(); ++posPlayer)
{
std::cout << (*posPlayer).GetFullName() << std::endl;
}
}

int main()
{
Game MyGame;
Player Temp;
Temp.SetFullName( "Jim" );
MyGame.InsertPlayer( Temp );
Temp.SetFullName( "Joe" );
MyGame.InsertPlayer( Temp );
Temp.SetFullName( "Bob" );
MyGame.InsertPlayer( Temp );
MyGame.ListPlayers();

std::cout << "Deleting..." << std::endl;
Temp.SetFullName("Joe");
MyGame.DropPlayer( Temp );

MyGame.ListPlayers();

char wait;
std::cin >> wait;
};
Nov 22 '05 #2
<A_*********@hotmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
....
for(posPlayer = playersActiveInRound.begin(); posPlayer !=
playersActiveInRound.end(); ++posPlayer)
{
if((*posPlayer).GetFullName() == targetPlayer.GetFullName())
{
playersActiveInRound.erase(posPlayer);
}
}


You must not increment the iterator if you remove an element from the
vector. Erase moves all elements after the removed one towards the vector's
front and the iterator will address the first element originally following
the removed one. Then, as part of the for statement, you move the iterator
to the next place in the vector. In the best case this will skip one
element. In the worst case, if the element at the end of the vector has been
removed, your iterator will never be equal to end().

There are several ways to solve your problem. If all the players have
different names and you always remove at most one player, the best way seems
to separate searching and removing. Something like

Iterator it = Find(playersActiveInRound, targetPlayer.GetFullName());
if (it != playersActiveInRound) playersActiveInRound.erase(it);

HTH
Heinz
Nov 22 '05 #3
A_*********@hotmail.com wrote:
hi,

my poker game does

playersActiveInRound = players;

at the start of every round where both are vector<Player> (i.e.,
containers of a user-defined class). the latter vector is everyone who
hasn't run out of money yet.

in a round, when someone folds I wish to remove them from
playersActiveInRound by calling

void Game::DropPlayer(Player& targetPlayer)
{

for(posPlayer = playersActiveInRound.begin(); posPlayer !=
playersActiveInRound.end(); ++posPlayer)
{
if((*posPlayer).GetFullName() == targetPlayer.GetFullName())
{
playersActiveInRound.erase(posPlayer);
}
}

}

and feeding it the Player identifier.

can anyone tell me why I get a crash everytime this function runs?

thx a lot


This is a case where you have to look at exactly what you wrote instead
of what you think you wrote.

Specifically what do you think happens to posPlayer when you erase a player

for(...; ++posPlayer)
{
if(...)
{
playersActiveInRound.erase(posPlayer);
}
}

Think about it, you are erasing a player and moving the iterator forward
in the same iteration.

The right way is like this

while (...)
{
if (...)
posPlayer = playersActiveInRound.erase(posPlayer);
else
++posPlayer;
}

erase returns an iterator to the element after the erased element.

john
Nov 22 '05 #4

ah. had nothing to do with references whatsoever!

thx for your help, all.

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
6
by: Billy Jacobs | last post by:
I have a website which has both secure and non-secure pages. I want to uses forms authentication. How do I accomplish this? Originally I had my web.config file in the root with Forms...
5
by: Stephan Hoffmann | last post by:
Hi, I use XML mainly as a source for HTML. HTML browsers 'know' certain entity references like &eacute; or &auml;. When I use XSL to transform XML to HTML or XML, these entities are replaced...
25
by: Steve Jorgensen | last post by:
Yup, Steve's full of tips, but hey, it makes him feel important, right? Ok, here goes. I've been trying to improve encapsulation by putting code in the same object as the stuff it affects, so I...
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
12
by: Brian Henry | last post by:
I made a header control in asp.net and it references images in the /images/ folder in the virtural path's root folder... How can I get the header control to show the images at any level of the web...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
0
by: RajaKannan | last post by:
I have an MS Access 2002 Application in which i am trying to use web service. As given in the microsoft site, i installed the "Web Reference Toolkit" and also included the "Microsoft SOAP Type...
17
by: Paul | last post by:
On my development computer, I have virtual named host set up, like www.site1.lab. When I upload those to my web site for customer review under mywebsite.com/clients/site1/ it throws some of the...
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.