I have the two classes Player and Team, where each player class has a pointer to the team
class Player{
private: Team* team;}
and each team has a list of pointers to the players
class Team{
private: list<Player*> playerlist;}
Now i want to find out in which team a player plays by adressing him in the list. Afterwards i want to be able to adress the team for something like team.getid()
I tried the following solution for a function:
std::list<Team>::iterator* teamsearch(std::list<Team> a, int b)
{
int i = 0;
std::list<Team>::iterator Listitem;
std::list<Team>::iterator* Listitemtest = &Listitem;
while (i == 0){
for (Listitem = a.begin(); Listitem != a.end(); ++Listitem)
{
if (Listitem->getteamID_() == b)
i++;
Listitemtest = &Listitem;
}
};
std::cout << "TeamID: " << (*Listitemtest)->getteamID_() << std::endl;
return Listitemtest;
}
The idea is that it returns an iterator which points to the adress of the team of the player. Is this correct ?
Then i tried to adress the teams by dereferencing the iterator again by this way:
Team team;
team.setteamID_(0);
team.setteamname_("team1");
Teamvector.push_back(team);
std::list<Team>::iterator Listitem;
Listitem = *teamsearch(Teamvector, 0);
which gives me a runtime error in MSVC120.dll ...include\list: list iterator not derefencable
Somehow it seems like the iterator points to the end of the teamlist ?