0
I’m doing a simplified game in the style of Pokémon, but is stopping to answer when calling Bootpokemon for the second time, I’m not finding the problem because it works normally with the first and hangs on the second.
The values print is just to test if everything is being passed correctly.
Excerpts from the code:
Main:
int main(int argc, char** argv){
TipoMove flamethrower;
TipoMove fire_blast;
TipoMove fly;
TipoMove earthquake;
TipoMove quick_attack;
TipoMove thunderbolt;
TipoMove thunder;
TipoMove skull_bash;
InicializaMove(&flamethrower, "Flamethrower", 1, 'e', 95);
InicializaMove(&fire_blast, "Fire Blast", 2, 'e', 120);
InicializaMove(&fly, "Fly", 3, 'f', 70);
InicializaMove(&earthquake, "Earthquake", 4, 'f', 100);
InicializaMove(&quick_attack, "Quick Attack", 5, 'f', 40);
InicializaMove(&thunderbolt, "Thunderbolt", 6, 'e', 95);
InicializaMove(&thunder, "Thunder", 7, 'e', 120);
InicializaMove(&skull_bash, "Skull Bash", 8, 'f', 100);
TipoPokemon charizard;
TipoPokemon pikachu;
InicializaPokemon(&charizard, 2, "Charizard", 293, 280, 360, 328, 348, 295, flamethrower, fire_blast, fly, earthquake);
ImprimePokemon(&charizard);
InicializaPokemon(&pikachu, 1, "pikachu", 229, 196, 274, 306, 218, 218, quick_attack, thunderbolt, thunder, skull_bash);
ImprimePokemon(&pikachu);
return 0;
}
Bootpokemon:
void InicializaPokemon(TipoPokemon *poke, int id, string nome, float ataque, float defesa, float vida, float velocidade, float spAtk, float spDef, TipoMove mov0, TipoMove mov1, TipoMove mov2, TipoMove mov3){
poke->id = id;
poke->nome = nome;
poke->atk = ataque;
poke->def = defesa;
poke->hp = vida;
poke->speed = velocidade;
poke->sp_atk = spAtk;
poke->sp_def = spDef;
poke->moves[0]= mov0;
poke->moves[1] = mov1;
poke->moves[2] = mov2;
poke->moves[3] = mov3;
}
Initialize:
void InicializaMove(TipoMove *mov, string n, int id, char t, float pw){
mov->nome = n;
mov->id = id;
mov->tipo = t;
mov->power = pw;
cout << "move " << mov->nome << " inicializado " << endl;
}
In the structure has
TipoMove moves[3];
, when it should be such4
, because you use 4 moves– Isac
@Isac but in C vectors start at position 0... C++ is not so either?
– Leila
Exact. In a size 3 vector, there are only positions 0, 1 and 2. There is no position 3. Hence the segmentation failure in your program.
– Cássio Renan
Wow, I changed here and it really worked, I ended up getting confused with this, thank you :D
– Leila
By the way so I can know how to solve in the future, what I did was paste the code in the codeblocks I ran and saw where crashed, which was on the line
poke->moves[3] = mov3;
, and by deduction if crasha ai is because the size does not play with the assigned values.– Isac
It would also be a good idea to start objects only in constructors, because there you guarantee that you are assigning values in objects that actually exist.
– RAM