getting error when erasing element from vector

It says that vector erase iterator outside range

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
Battle.cpp

#include "Battle.h"
#include "SetUp.h"



Battle::Battle(){

	
	SetUp _setUp;

	
	
	p1 = _setUp.getPlayer(1);
	p2 = _setUp.getPlayer(2);
	cur_player = p1;
	p1.setChoosenMove(p1.getPokeman().getMoves()[0]);
	p2.setChoosenMove(p2.getPokeman().getMoves()[0]);
	system("cls");
	cout << "\n**********LET THE BATTLE BEGIN************\n\n\n";
	system("PAUSE");
	system("cls");
	
	while (p1.getPokeman().getHealth() > 0 && p2.getPokeman().getHealth() > 0){
		printBattle();
	}
	cout << "Fight has ended";

	
	return;
}

void Battle::printBattle(){
	system("cls");
	Moves move;
	cout << p1.getName() << " with " << p1.getPokemanName() << "...................................................." << "hp:" << p1.getPokeman().getHealth() << endl << endl << endl << endl << "     [" << move.getName(p1.getChoosenMove()) << "]" << endl << "" << move.getName(p1.getOtherMoves(p1)[0]) << "  " << move.getName(p1.getOtherMoves(p1)[1]) << "  " << move.getName(p1.getOtherMoves(p1)[2]) << endl << endl << endl << endl << endl << endl << endl << endl << p2.getName() << " with " << p2.getPokemanName() << "...................................................." << "hp:" << p2.getPokeman().getHealth() << endl << endl << endl << endl << "     [" << move.getName(p2.getChoosenMove()) << "]" << endl << "" << move.getName(p2.getOtherMoves(p2)[0]) << "  " << move.getName(p2.getOtherMoves(p2)[1]) << "  " << move.getName(p2.getOtherMoves(p2)[2]);;
	//setChoosenMove(cur_player);
	setChoosenMove(p1);

	system("cls");
	return;

}

void Battle::setChoosenMove(Player& pl){
	Moves move;

	string input;
	
	cin >> input;
	
	if (input == "d" || input == "a" || input == "s"){
		if (input == "s"){
			pl.setChoosenMove(pl.getOtherMoves(pl)[1]);
			pl.getOtherMoves(pl).erase(pl.getOtherMoves(pl).begin() + pl.getChoosenMoveNum(pl.getChoosenMove()));
			input = "";
		}
		if (input == "a"){
			pl.setChoosenMove(pl.getOtherMoves(pl)[0]);
			pl.getOtherMoves(pl).erase(pl.getOtherMoves(pl).begin() + pl.getChoosenMoveNum(pl.getChoosenMove()));
			input = "";
		}
		if (input == "d"){
			pl.setChoosenMove(pl.getOtherMoves(pl)[2]);
			pl.getOtherMoves(pl).erase(pl.getOtherMoves(pl).begin() + pl.getChoosenMoveNum(pl.getChoosenMove()));
			input = "";
		}
	}
	else{
		cout << "bad Input";
	}

	
	
	return;
}






Moves.cpp

#include "Moves.h"



Moves::Moves(string name, float acu, int damage)
{
	_name = name;

}
Moves::Moves(){


}

void Moves::setMoves(vector <Moves> moves){

	
	
	_moves = moves;
}

vector<Moves> Moves::getMoves (){
	return _moves;

}


Moves Moves::movesList(int index){
	vector <Moves> movesList;
	Moves WaterBlast("WaterBlast", 100, 50);
	movesList.push_back(WaterBlast);
	Moves Scratch("Scratch", 100, 50);
	movesList.push_back(Scratch);
	Moves Fireball("Fireball", 100, 50);
	movesList.push_back(Fireball);
	Moves Ram("Ram", 100, 50);
	movesList.push_back(Ram);
	Moves SC("Scare", 100, 50);
	movesList.push_back(SC);
	Moves PK("Peck", 100, 50);
	movesList.push_back(PK);
	Moves CnTrT("Consentrate", 100, 50);
	movesList.push_back(CnTrT);
	Moves SA("SandAttack", 100, 50);
	movesList.push_back(SA);
	Moves DdG("Dodge", 100, 50);
	movesList.push_back(DdG);
	Moves LW("LeafWind", 100, 50);
	movesList.push_back(LW);
	Moves ShK("Shock", 100, 50);
	movesList.push_back(ShK);
	Moves Blst("Blast", 100, 50);
	movesList.push_back(Blst);

	return movesList[index - 1];
}

string Moves::getName(Moves move){
	return move._name;
}

bool Moves::areNotMovesEqual(Moves move1, Moves move2){
	if (move1.getName(move1) == move2.getName(move2)){
		return false;
	}
	else{
		return true;

	}
}





Player.cpp

#include "Player.h"


Player::Player(string name)
{
	_name = name;
}
Player::Player(){

}
void Player::setPokeman(Pokemans pokeman){
	_pokeman = pokeman;
}

Pokemans Player::getPokeman(){
	return _pokeman;
}

string Player::getPokemanName(){
	return _pokeman.getName();
}

string Player::getName(){
	return _name;
}

void Player::setPlayerName(string name){


	return;

}


Moves Player::getChoosenMove(){
	return _choosenMove;
}

vector <Moves> Player::getOtherMoves(Player player){
	string name = _choosenMove.getName(_choosenMove);
	Moves move;
	


	if (name == move.getName(player.getPokeman().getMoves()[0])){
		_otherMoves.push_back(player.getPokeman().getMoves()[1]);
		_otherMoves.push_back(player.getPokeman().getMoves()[2]);
		_otherMoves.push_back(player.getPokeman().getMoves()[3]);
		

	}
	if (name == move.getName(player.getPokeman().getMoves()[1])){
		_otherMoves.push_back(player.getPokeman().getMoves()[0]);
		_otherMoves.push_back(player.getPokeman().getMoves()[2]);
		_otherMoves.push_back(player.getPokeman().getMoves()[3]);
		

	}
	if (name == move.getName(player.getPokeman().getMoves()[2])){
		_otherMoves.push_back(player.getPokeman().getMoves()[0]);
		_otherMoves.push_back(player.getPokeman().getMoves()[1]);
		_otherMoves.push_back(player.getPokeman().getMoves()[3]);
	

	}
	if (name == move.getName(player.getPokeman().getMoves()[3])){
		_otherMoves.push_back(player.getPokeman().getMoves()[0]);
		_otherMoves.push_back(player.getPokeman().getMoves()[1]);
		_otherMoves.push_back(player.getPokeman().getMoves()[2]);


	}

	
	
	return _otherMoves;
}

void Player::setChoosenMove(Moves move){
	_choosenMove = move;
	
	return;

}

int Player::getChoosenMoveNum(Moves move){
	int in = 0;
	for (int i = 0; move.areNotMovesEqual(move, _otherMoves[i]); i++){

		in = i;
	}
	return in;
}





It happens rigth after the function areNotMovesEqual return true
The problem has to do with that getOtherMoves returns a copy of the vector.
Last edited on
I already tried to just erase 1 element by hard codding it and it works.
actually now that you say it, it seems like it only returns a move that has the same name, but not the same stats.
what should i do to make it return the move
Each time you call the getOtherMoves function, the code of that function will run.

Not sure that function inserts any elements to _otherMoves because the move object that you create on line 203 is a new Moves object with an empty name. If you are not adding anything to _otherMoves elsewhere it probably means it is empty.

Edit: I was a bit confused by your getName function. It doesn't make sense that you have to pass another Moves object as argument to it. Just make it return the name of the current object, like you do for the Player class, and then just call it on the object that you want to get the name of:
1
2
3
Moves move;
if (name == move.getName(player.getPokeman().getMoves()[0])){
if (name == player.getPokeman().getMoves()[0].getName()){

You are doing things like pl.getOtherMoves(pl)[1] which will not work unless the vector has at least 2 elements. Just saying, I don't know if it's always the case.

The vector that is returned is a copy of _otherMoves because the return type of the function is vector<Moves>. If you don't want a copy, then you probably want to return a reference instead (vector<Moves>&).
Last edited on
thanks for the advice, thought I dont knon why I made it that way to begin with :D
Sadly it doesnt fix the errror, so if you have any other ideas I would love to hear them
I noticed that the _otherMoves gets to 9 or 12 element which are the correct ones but looped
fixed the _otherMoves problem, it happend because everytime I called getOtherMoves function it would add more moves to the vector. Now I just clear the vector everytime it goes throught the code.
Fixed!!!!! I didnt even have to erase anything, because I already had a function that updates the _otherMoves and that is getOtherMoves(). here is what I changed


Battle.cpp
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
#include "Battle.h"
#include "SetUp.h"



Battle::Battle(){

	
	SetUp _setUp;

	
	
	p1 = _setUp.getPlayer(1);
	p2 = _setUp.getPlayer(2);
	cur_player = p1;
	p1.setChoosenMove(p1.getPokeman().getMoves()[0]);
	p2.setChoosenMove(p2.getPokeman().getMoves()[0]);
	system("cls");
	cout << "\n**********LET THE BATTLE BEGIN************\n\n\n";
	system("PAUSE");
	system("cls");
	
	while (p1.getPokeman().getHealth() > 0 && p2.getPokeman().getHealth() > 0){
		printBattle();
	}
	cout << "Fight has ended";

	
	return;
}

void Battle::printBattle(){
	system("cls");
	Moves move;
	cout << p1.getName() << " with " << p1.getPokemanName() << "...................................................." << "hp:" << p1.getPokeman().getHealth() << endl << endl << endl << endl << "     [" << p1.getChoosenMove().getName() << "]" << endl << "" << p1.getOtherMoves(p1)[0].getName() << "  " << p1.getOtherMoves(p1)[1].getName() << "  " << p1.getOtherMoves(p1)[2].getName() << endl << endl << endl << endl << endl << endl << endl << endl << p2.getName() << " with " << p2.getPokemanName() << "...................................................." << "hp:" << p2.getPokeman().getHealth() << endl << endl << endl << endl << "     [" << p2.getChoosenMove().getName() << "]" << endl << "" << p2.getOtherMoves(p2)[0].getName() << "  " << p2.getOtherMoves(p2)[1].getName() << "  " << p2.getOtherMoves(p2)[2].getName();
	//setChoosenMove(cur_player);
	setChoosenMove(p1);

	system("cls");
	return;

}

void Battle::setChoosenMove(Player& pl){
	

	string input;
	
	cin >> input;
	
	if (input == "d" || input == "a" || input == "s"){
		
		if (input == "a"){
			pl.setChoosenMove(pl.getOtherMoves(pl)[0]);
			
			pl.getOtherMoves(pl);
			
			cout << "erased";
			input = "";
		}
		if (input == "s"){
			pl.setChoosenMove(pl.getOtherMoves(pl)[1]);
			
			
			pl.getOtherMoves(pl);
			cout << "erased";
			input = "";
		}
		if (input == "d"){
			pl.setChoosenMove(pl.getOtherMoves(pl)[2]);
			
			
			pl.getOtherMoves(pl);
			cout << "erased";
			input = "";
		}
	}
	else{
		cout << "bad Input";
	}

	
	
	return;
}









Player.cpp
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
#include "Player.h"


Player::Player(string name)
{
	_name = name;
}
Player::Player(){

}
void Player::setPokeman(Pokemans pokeman){
	_pokeman = pokeman;
}

Pokemans Player::getPokeman(){
	return _pokeman;
}

string Player::getPokemanName(){
	return _pokeman.getName();
}

string Player::getName(){
	return _name;
}

void Player::setPlayerName(string name){


	return;

}


Moves Player::getChoosenMove(){
	return _choosenMove;
}

vector <Moves> Player::getOtherMoves(Player player){
	string name = _choosenMove.getName();
	Moves move;
	
	_otherMoves.clear();

	if (name == player.getPokeman().getMoves()[0].getName()){
		_otherMoves.push_back(player.getPokeman().getMoves()[1]);
		_otherMoves.push_back(player.getPokeman().getMoves()[2]);
		_otherMoves.push_back(player.getPokeman().getMoves()[3]);
		

	}
	if (name == player.getPokeman().getMoves()[1].getName()){
		_otherMoves.push_back(player.getPokeman().getMoves()[0]);
		_otherMoves.push_back(player.getPokeman().getMoves()[2]);
		_otherMoves.push_back(player.getPokeman().getMoves()[3]);
		

	}
	if (name == player.getPokeman().getMoves()[2].getName()){
		_otherMoves.push_back(player.getPokeman().getMoves()[0]);
		_otherMoves.push_back(player.getPokeman().getMoves()[1]);
		_otherMoves.push_back(player.getPokeman().getMoves()[3]);
	

	}
	if (name == player.getPokeman().getMoves()[3].getName()){
		_otherMoves.push_back(player.getPokeman().getMoves()[0]);
		_otherMoves.push_back(player.getPokeman().getMoves()[1]);
		_otherMoves.push_back(player.getPokeman().getMoves()[2]);


	}

	
	
	return _otherMoves;
}

void Player::setChoosenMove(Moves move){
	_choosenMove = move;
	
	return;

}

int Player::getChoosenMoveNum(Moves move){
	int in = 0;
	for (in; move.areNotMovesEqual(move, _otherMoves[in]); in++){

		
		
		
	}
	cout << _otherMoves[in].getName();
	return in;
}


Thanks for the help
Topic archived. No new replies allowed.