mercredi 20 février 2019

accessing values within map of class objects gives segmentation fault

I have a class team:

class team
{
public:
        int matches;
        int goals_scored;
        int goals_against;
        int wins;
        int losses;
        int points;

        team()
        {
                matches = goals_scored = goals_against = wins = losses = points = 0;
        }
};

I have created a map :

 map<string,team*> record;

I have a fixed number of teams, so i first create the map with all the team names and initiated team objects.

    for(i=0;i<teams;i++)
            {
                    getline(cin,team_name);
                    record[team_name] = new team();
            }

Then sometime later I fill them when the match happens (it is sure that no new team name comes, other than the ones initialized before) :

                    team *t1 = record[team1_name]; //get the pointer to fill
                    team *t2 = record[team2_name];
                    if(score1 > score2)
                    {
                            t1->points += 3;  <--------- crashes here
                            t1->wins++;
                            t2->losses++;
                    }

Why I am getting a crash?

Aucun commentaire:

Enregistrer un commentaire