Issue dereferencing iterator

I am getting a debug assertion when running playBall. expression: list iterator not dereferenceable.

The getTeam function recognizes the teams I want it to from the list. When I try to pass in the defined iterator it crashes out. I am new to using STL list and very new to iterators.

I am only including the code for the Game class. Standings is working fine. This is all code I have written. Any Help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <list>
#include <algorithm>
#include "standings.h"

using namespace std;

#define NAMELENGTH 20

class Game{

private:
	Standings *stats;
	list<Records> statsList;
	list<Records>::iterator team1;
	list<Records>::iterator team2;
	char teamName1[NAMELENGTH];
	char teamName2[NAMELENGTH];
	int teamDivision1;
	int teamDivision2;

	
public:
	
	Game(Standings *s);
	~Game();

	void getTeam(char[], list<Records>::iterator,int );
	void playBall();
	void printTeam(list<Records>::iterator);

};

#endif

//game.cpp
#include "game.h"

Game::Game(Standings* s)
{
	stats = s;

}

Game::~Game()
{

}

void Game::getTeam(char teamNameInput[], list<Records>::iterator team, int teamDivision)
{ 
	statsList = stats->recordsList;
	
	cout << "Please type the wanted teams name. It must match the name in the standings." << endl;
	cin >> teamNameInput;

	cout << "Please enter the value of the division. " << endl;
	cin >> teamDivision;

	for (team = statsList.begin(); team != statsList.end(); team++){
		if (!strcmp(team->teamName, teamNameInput)&& (team->division == teamDivision)){
			return;	
		}
	}
		cout << "Team not found. Re-enter the Teams info.\n";
		getTeam(teamNameInput, team,teamDivision);
		return;
} 

void Game::printTeam(list<Records>::iterator team)
{
	cout  << team->teamName << setw(10) << team->totalWin << " " << team->totalLose << " " << team->winPercentage << " " << team->gamesBehind<< " "
			<< team->streakWin << " " << team->streakLose << " " << team->streakLength << " " << team->streakType << " "
			<< team->homeWin << " " << team->homeLose << " " << team->awayWin << " "  << team->awayLose << " " << team->interWin << " "
			<< team->interLose << " " << team->division;
		cout << endl;
/*
		//Printing Team 2
cout  << team2->teamName << setw(10)<< team2->totalWin << " " << team2->totalLose << " " << team2->winPercentage << " " << team2->gamesBehind<< " "
			<< team2->streakWin << " " << team2->streakLose << " " << team2->streakLength << " " << team2->streakType << " "
			<< team2->homeWin << " " << team2->homeLose << " " << team2->awayWin << " "  << team2->awayLose << " " << team2->interWin << " "
			<< team2->interLose << " " << team2->division;
		cout << endl;*/
}

void Game::playBall()
{
getTeam(teamName1, team1, teamDivision1);
getTeam(teamName2, team2, teamDivision2);
printTeam(team1);
printTeam(team2);
}


The printing team 2 code is commented out because it worked but I wanted to get rid of the unneeded code.
Ok I fixed the above issue. Thanks again!
Topic archived. No new replies allowed.