IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++/CLI Discussion :

Problème de Violation d'accès lors de la lecture de l'emplacement


Sujet :

C++/CLI

  1. #1
    Débutant
    Homme Profil pro
    aucun
    Inscrit en
    Avril 2012
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Avril 2012
    Messages : 152
    Points : 28
    Points
    28
    Par défaut 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 :
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
    }

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2005
    Messages
    5 157
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2005
    Messages : 5 157
    Points : 12 271
    Points
    12 271
    Par défaut
    Numéro de ligne et nom du fichier du code source où ça crash, SVP.

  3. #3
    Débutant
    Homme Profil pro
    aucun
    Inscrit en
    Avril 2012
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Avril 2012
    Messages : 152
    Points : 28
    Points
    28
    Par défaut
    Ce n'est pas indiqué. Ils m'ont juste envoyé cela :
    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.

  4. #4
    Membre émérite
    Avatar de Daïmanu
    Homme Profil pro
    Développeur touche à tout
    Inscrit en
    Janvier 2011
    Messages
    711
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur touche à tout

    Informations forums :
    Inscription : Janvier 2011
    Messages : 711
    Points : 2 346
    Points
    2 346
    Par défaut
    À vue de nez je dirai que c'est la variable membre m_animal de Personnage qui n'est jamais initialisée, donc la ligne m_animal->mourrirAnimal(); plante.

    Par ailleurs, Animal et Personnage ont beaucoup de méthodes / attributs en commun, il faudrait envisager de créer une classe commune dont Animal et Personnage hériteraient.

  5. #5
    Débutant
    Homme Profil pro
    aucun
    Inscrit en
    Avril 2012
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Avril 2012
    Messages : 152
    Points : 28
    Points
    28
    Par défaut
    Oui, j'ai bel et bien initialisé la variable membre m_animal.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    ....
    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();
    }
    ....

  6. #6
    Membre émérite
    Avatar de Daïmanu
    Homme Profil pro
    Développeur touche à tout
    Inscrit en
    Janvier 2011
    Messages
    711
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur touche à tout

    Informations forums :
    Inscription : Janvier 2011
    Messages : 711
    Points : 2 346
    Points
    2 346
    Par défaut
    En effet, je n'avais pas vu que les constructeurs sont définis en bas de Personnage.cpp.

    Cela dit, c'est pourtant bien ça le problème, il n'est pas initialisé dans le constructeur Personnage(std::string, int).

    Petit détail aussi, dans main.cpp tu inclues Personnage.h et dans Personnage.cpp tu inclues personnage.h. Je suppose que tu développes sous Windows donc ça ne te pose pas de problème, mais si tu veux distribuer ton programme ça en sera un lors la compilation.

  7. #7
    Débutant
    Homme Profil pro
    aucun
    Inscrit en
    Avril 2012
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : aucun
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Avril 2012
    Messages : 152
    Points : 28
    Points
    28
    Par défaut
    Merci beaucoup. Ça fonctionne.

  8. #8
    Expert éminent sénior
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2005
    Messages
    5 157
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2005
    Messages : 5 157
    Points : 12 271
    Points
    12 271
    Par défaut
    Comme vous êtes au début, ne prenez pas cette sale habitude d'utiliser des pointeurs nus. Utilisez des pointeurs intelligents, ça simplifiera beaucoup vos programmes.
    Et ici, le plus simple, c'est de ne pas utiliser de pointeur du tout.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 11
    Dernier message: 01/05/2015, 13h58
  2. Réponses: 6
    Dernier message: 14/08/2013, 11h57
  3. Réponses: 2
    Dernier message: 09/11/2010, 21h51
  4. Réponses: 5
    Dernier message: 01/12/2009, 01h06
  5. Réponses: 7
    Dernier message: 13/08/2009, 09h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo