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
    Membre tr�s actif
    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
    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 confirm�
    Homme Profil pro
    D�veloppeur informatique
    Inscrit en
    F�vrier 2005
    Messages
    5 503
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 53
    Localisation : France, Val de Marne (�le de France)

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

    Informations forums :
    Inscription : F�vrier 2005
    Messages : 5 503
    Par d�faut
    Num�ro de ligne et nom du fichier du code source o� �a crash, SVP.

  3. #3
    Membre tr�s actif
    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
    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
    736
    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 : 736
    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
    Membre tr�s actif
    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
    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
    736
    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 : 736
    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
    Membre tr�s actif
    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
    Par d�faut
    Merci beaucoup. �a fonctionne.

  8. #8
    Expert confirm�
    Homme Profil pro
    D�veloppeur informatique
    Inscrit en
    F�vrier 2005
    Messages
    5 503
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 53
    Localisation : France, Val de Marne (�le de France)

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

    Informations forums :
    Inscription : F�vrier 2005
    Messages : 5 503
    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