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