Problème de Violation d'accès lors de la lecture de l'emplacement
Bonjour à tous,
Je suis actuellement en train d'apprendre le c++. Je me pratiques de fur et à mesure que j'évolue. Au ce moment, j'essaie de faire un mini-rpg. Mais, j'ai un problème qui affiche :
Citation:
Exception de première chance à 0x00bc3586 dans Tutorial.exe*: 0xC0000005: Violation d'accès lors de la lecture de l'emplacement 0x00000008.
Exception non gérée à 0x00bc3586 dans Tutorial.exe*: 0xC0000005: Violation d'accès lors de la lecture de l'emplacement 0x00000008.
Animal.h
Code:
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
| #ifndef ANIMAL_H_INCLUDED
#define ANIMAL_H_INCLUDED
#include <string>
class Animal
{
public:
Animal();
Animal(std::string nom);
bool estVivantAnimal() const;
int getForceAnimal() const;
void mourrirAnimal();
void changerNiveauAnimal();
int getVieAnimal() const;
std::string getNomAnimal() const;
int getNiveauAnimal() const;
private:
int m_vieAnimal;
int m_forceAnimal;
int m_niveauAnimal;
bool m_enVieAnimal;
std::string m_nomAnimal;
};
#endif |
Personnage.h
Code:
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
| #ifndef PERSONNAGE_H_INCLUDED
#define PERSONNAGE_H_INCLUDED
#include "Animal.h"
#include <string>
class Personnage
{
public:
Personnage(std::string nom);
Personnage();
~Personnage();
Personnage(std::string nom,int niveau);
void recevoirDegat(int nbDegat);
void attaquer(Personnage &cible);
void attaqueSpecial(Personnage &cible);
void attaqueAnimal(Personnage &cible);
bool estVivant() const;
void changerNiveau();
void etat() const;
std::string getNom() const;
private:
int m_vie;
int m_energie;
int m_force;
int m_forceSpecial;
bool m_enVie;
int m_niveau;
std::string m_nom;
Animal *m_animal;
};
#endif |
Animal.cpp
Code:
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
| #include "Animal.h"
Animal::Animal() : m_vieAnimal(50), m_forceAnimal(2), m_niveauAnimal(1), m_enVieAnimal(true), m_nomAnimal("lil dog")
{
}
Animal::Animal(std::string nom) : m_vieAnimal(50), m_forceAnimal(2), m_niveauAnimal(1), m_enVieAnimal(true), m_nomAnimal(nom)
{
}
int Animal::getForceAnimal() const
{
return m_forceAnimal;
}
void Animal::changerNiveauAnimal()
{
m_niveauAnimal++;
m_vieAnimal = 50+50*m_niveauAnimal;
m_forceAnimal += 1;
}
bool Animal::estVivantAnimal() const
{
return m_enVieAnimal;
}
void Animal::mourrirAnimal()
{
m_vieAnimal = 0;
m_enVieAnimal = false;
}
std::string Animal::getNomAnimal() const
{
return m_nomAnimal;
}
int Animal::getNiveauAnimal() const
{
return m_niveauAnimal;
}
int Animal::getVieAnimal() const
{
return m_vieAnimal;
} |
Personnage.cpp
Code:
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
| #include "personnage.h"
#include <iostream>
using namespace std;
void Personnage::recevoirDegat(int nbDegat)
{
m_vie -= nbDegat;
cout << m_nom << " a perdu " << nbDegat << " vies." << endl;
if(m_vie <= 0)
{
m_animal->mourrirAnimal();
m_vie = 0;
m_enVie = false;
cout << m_nom << " est mort" << endl;
}
}
void Personnage::attaquer(Personnage &cible)
{
cible.recevoirDegat(m_force);
if(!cible.estVivant())
{
changerNiveau();
}
cout << m_nom << " a attaque " << cible.getNom() << endl;
}
void Personnage::attaqueSpecial(Personnage &cible)
{
if(m_energie >= 20)
{
cible.recevoirDegat(m_forceSpecial);
m_energie -= 20;
cout << m_nom << " a faite une superattaque sur " << cible.getNom() << endl;
if(!cible.estVivant())
{
changerNiveau();
}
}
else
{
cout << m_nom << " n'a plus d'energie" << endl;
}
}
bool Personnage::estVivant() const
{
return m_enVie;
}
void Personnage::changerNiveau()
{
m_niveau++;
m_vie = 100+50*m_niveau;
m_energie = 100;
m_force += 1;
m_forceSpecial += 5;
cout << m_nom << " est maintenant niveau " << m_niveau << endl;
}
Personnage::Personnage() : m_vie(100), m_energie(100), m_force(5), m_forceSpecial(25),m_enVie(true), m_niveau(1), m_nom("Big Young lil g"), m_animal(0)
{
m_animal = new Animal();
}
Personnage::Personnage(string nom) : m_vie(100), m_energie(100), m_force(5), m_forceSpecial(25),m_enVie(true), m_niveau(1), m_nom(nom), m_animal(0)
{
m_animal = new Animal();
}
Personnage::Personnage(string nom,int niveau) : m_vie(50+50*niveau), m_energie(50+50*niveau), m_force(4+niveau), m_forceSpecial(20+5*niveau),m_enVie(true), m_niveau(niveau), m_nom(nom), m_animal()
{
}
void Personnage::attaqueAnimal(Personnage &cible)
{
if(m_animal->getVieAnimal() > 0)
{
cible.recevoirDegat(m_animal->getForceAnimal());
if(!cible.estVivant())
{
m_animal->changerNiveauAnimal();
}
cout << m_animal->getNomAnimal() << " a attaque " << cible.getNom() << endl;
}
}
void Personnage::etat() const
{
cout << "----------------------------------" << endl;
cout << "Nom : " << m_nom << " niv. " << m_niveau << endl;
cout << "Vie : " << m_vie << endl;
cout << "Energie : " << m_energie << endl;
cout << "Animal : " << m_animal->getNomAnimal() << " | vie: " << m_animal->getVieAnimal() << " | Niv.: " << m_animal->getNiveauAnimal() << endl;
}
std::string Personnage::getNom() const
{
return m_nom;
}
Personnage::~Personnage()
{
delete m_animal;
} |
main.cpp
Code:
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
| #include <iostream>
#include <string>
#include "Personnage.h"
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
string nom("");
int tour(0);
int ordinateur(0);
string action("");
cout << "Nom de votre Personnage : ";
getline(cin,nom);
Personnage joueur(nom), monstre;
do
{
cout << "------------------Nouvelle Partie----------------------" << endl;
tour ++;
Personnage monstre("Un Monstre",tour);
do
{
joueur.etat();
monstre.etat();
cout << "z = Attaque - x = Attaque Special - c = Utiliser l'animal" << endl;
getline(cin,action);
if(action == "z")
{
joueur.attaquer(monstre);
}
else if(action == "x")
{
joueur.attaqueSpecial(monstre);
}
else if(action == "c")
{
joueur.attaqueAnimal(monstre);
}
else
{
cout << "L'attaque de " << joueur.getNom() << " a ete une echec" << endl;
}
ordinateur = rand() % 3;
switch(ordinateur)
{
case 0:
monstre.attaquer(joueur);
break;
case 1:
monstre.attaqueSpecial(joueur);
break;
case 2:
monstre.attaqueAnimal(joueur);
break;
case 3:
cout << "L'attaque de " << monstre.getNom() << " a ete une echec" << endl;
break;
}
cout << endl << endl << endl << endl << endl;
}while(joueur.estVivant() && monstre.estVivant());
}while(joueur.estVivant());
cout << "Game Over" << endl;
system("PAUSE");
return 0;
} |