Getting Segmentation fault using Maps!!

GameMaster.h
#includes ...
class GameMaster
{
public:
void GameSetup();
void PlayGame();
private:
GameSpace::MapSquadType MapOfSquads;​
GameSpace::SquadType Squad(int x);

void MakeSquad(char type, string name, int coordX, int coordY, char dir, int squad);
Player::PlayerType SwitchType(char type);

void BoardSetUp(); void LoadPlayers(); void LoadQueue(); void LoadArmament(); void GameCleanUp(); oid PreTurn(); void BugTurn(); void HumanTurn(); void PostTurn(); void GameOver();

};​

GameMaster.cpp

int main()
{
GameMaster * Game;
Game -> GameSetup();
Game -> PlayGame();
}

void GameMaster::GameSetup()
{
LoadPlayers();
}

void LoadPlayers()
{
char type , dir;
string name;
int coordX , coordY , squad = 0;

ifstream inFile;
inFile.open(GameSpace::PLAYERS_FILE.c_str());
while(!inFile.eof()){
inFile >> type;

if(type == 'q' || type == 'Q')
squad++;

if(!inFile)
break;

while(type!='q' && type!='Q'){
inFile>> name >> coordX >> coordY >> dir;
MakeSquad( type , name , coordX , coordY , dir , squad , MapOfSquads);
inFile >> type;
if(type == 'q' || type == 'Q')
squad++;
}
}

}

void MakeSquad(char type, string name, int coordX, int coordY, char dir, int squad)
{
MapOfSquads.insert(make_pair(name , Squad(squad)); // Squad( int ) just converts the int squad number to the Enum.
}

I get the segmentation fault whenever it tries to insert the first player.. however whenever i do this :

void MakeSquad(char type, string name, int coordX, int coordY, char dir, int squad)
{
GameSpace::MapSquadType MapOfSquads;​
MapOfSquads.insert(make_pair(name , Squad(squad)); // Squad( int ) just converts the int squad number to the Enum.
}

I do not get any errors or segmentation faults..
closed account (3hM2Nwbp)
1
2
3
4
5
6
int main()
{
GameMaster * Game;
Game -> GameSetup(); // <-- Dereferencing a dirty pointer.
Game -> PlayGame();
}


Why use a pointer at all?

1
2
3
GameMaster game;
game.GameSetup();
game.PlayGame();
Last edited on
either way works.. i could definately do it that way, it doesn't change my issue :P...
closed account (3hM2Nwbp)
Two cases:

1) The code you've posted is not the code that you're running.
2) Your compiler toolchain is corrupt.

I'll assume it's case #1, and will need the exact copy & pasted code.

On the above code there are numerous compile-time errors including syntax errors and missing types.
i would post all the code but there is thousands upon thousands of lines and 20+ files.. >.<
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
#ifndef GameMasterX
#define GameMasterX
//GameMaster

#include "p5.h"
#include "gamespace.h"
#include "phc.h"
#include "board.h"
#include "dice.h"
#include "cell.h"

using namespace std;

class GameMaster
{
	public:
		void GameSetup();
		void PlayGame();
	private:
		GameSpace::MapSquadType MapOfSquads;
	
		GameSpace::SquadType Squad(int x);
		void MakeSquad(char type, string name, int coordX, int coordY, char dir, int squad);
		Player::PlayerType SwitchType(char type);
		VecPlayerType Alpha;
		void BoardSetUp();
		void LoadPlayers();
		void LoadQueue();
		void LoadArmament();
		void GameCleanUp();
		void PreTurn();
		void BugTurn();
		void HumanTurn();
		void PostTurn();
		void GameOver();
		
};

#endif 


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//GameMaster

#include "p5.h"
#include "phc.h"
#include "gamespace.h"
#include "board5.h"
#include "dice.h"
#include "cell.h"
#include<string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <vector>
#include <algorithm>


#include "GameMaster.h"


using namespace std;
using namespace GameSpace;

int main()
{
	Dice::ReadRollsFromFile();
	
	GameMaster Game;
	Game.GameSetup();
	Game.PlayGame();

	return 0;
}

void GameMaster::GameSetup()
{
	
	cout << endl << "Game Setup Initiating..." << endl;
}

void GameMaster::PlayGame()
{
	BoardSetUp();
	LoadPlayers();
	LoadQueue();
	LoadArmament();	
}

void GameMaster::BoardSetUp()
{
	Board::CreateBoard(GameSpace::NUMROWS , GameSpace::NUMCOLS);
	cout << endl << "Setting Up Board...";
}


void GameMaster::LoadPlayers()
{
	char type , dir;
	string name;
	int coordX , coordY , squad = 0;
	GameSpace::SquadType squadName;
	
	cout << endl << "Loading Players...";

	
	ifstream inFile;
	inFile.open(GameSpace::PLAYERS_FILE.c_str());
	while(!inFile.eof()){	
		inFile >> type;
		if(type == 'q' || type == 'Q')
			squad++;
		if(!inFile)
			break;
		while(type!='q' && type!='Q'){
			inFile>> name >> coordX >> coordY >> dir;
			MakeSquad( type , name , coordX , coordY , dir , squad );
			inFile >> type;
			if(type == 'q' || type == 'Q')
				squad++;
		}
	}

	inFile.close();
}

void GameMaster::LoadQueue()
{
	cout << endl << "Loading Queue...";
}

void GameMaster::LoadArmament()
{
	cout << endl << "Loading Armanent...";
}

void GameMaster::GameCleanUp()
{
}

void GameMaster::PreTurn()
{
}

void GameMaster::BugTurn()
{
}

void GameMaster::HumanTurn()
{
}

void GameMaster::PostTurn()
{
}

void GameMaster::GameOver()
{
}

GameSpace::SquadType GameMaster::Squad(int x)
{
	GameSpace::SquadType Squad;
	switch(x)
	{
		case 0 : return ALPHA;
			break;
		case 1 : return BRAVO;
			break;
		case 2 : return CHARLIE;
			break;
		case 3 : return DELTA;
			break;
		case 4 : return ECHO;
			break;
	}


}

void GameMaster::MakeSquad(char type, string name, int coordX, int coordY, char dir, int squad)
{
     //This is where i'm getting the segmentation fault.

	MapOfSquads.insert(make_pair(name , Squad(squad));
}

Player::PlayerType GameMaster::SwitchType(char type)
{
	switch(type)
	{
		case 'w' : return Player::WARRIOR;
			break;
		case 'e' : return Player::EXPLORER;
			break;
		case 's' : return Player::SPIDER;
			break;	
		case 'h' : return Player::HORNET;
			break;
		case 'k' : return Player::KILLERANT;
			break;
		default : return Player::EXPLORER;
	
	}

}
Topic archived. No new replies allowed.