bonsoir à tous.
Je suis sur le cours de c++ qui explique comment utiliser les class.
Mon problème c'est que code::blocks dit qu'il y a une erreur et je ne sais pas se que cela veux dire.
J'ai suivi le cours de matheo et relus plusieurs fois, mais je ne vois pas où se situe l'erreur.
Voilà l'erreur que code::blocks m'annonce au moment de la compilation:
Citation : code::blocksPersonnage.cpp: In constructor `Personnage::Personnage(std::string, int)':
Personnage.cpp:20: error: member initializer expression list treated as compound expression
Personnage.cpp:20: error: invalid conversion from `int' to `Arme*'
Voici le code:
Personnage.h
Code : C++#ifndef DEF_PERSONNAGE
#define DEF_PERSONNAGE
#include "Arme.h"
class Personnage
{
public:
Personnage();
Personnage(const Personnage &personnageACopier);//constructeur de copie
Personnage(std::string nomArme, int degatsArme);
~Personnage();
void recevoirDegats(int nbDegats);
void attaquer(Personnage &cible);
void boirePotionDeVie(int quantitePotion);
void changerArme(std::string nomNouvelleArme, int degatsNouvelleArme);
bool estVivant();
void afficherEtat();
private:
int m_vie;
int m_mana;
Arme *m_arme;
};
#endif
Personnage.cpp
Code : C++
#include <string>
#include "Personnage.h"
using namespace std;
Personnage::Personnage() : m_vie(100), m_mana(100)
{
m_arme = new Arme();
}
Personnage::Personnage(const Personnage &personnageACopier)
{
m_vie = personnageACopier.m_vie;
m_mana = personnageACopier.m_mana;
m_arme = new Arme(*(personnageACopier.m_arme));
}
Personnage::Personnage(string nomArme, int degatsArme) : m_vie(100), m_mana(100), m_arme(nomArme, degatsArme) // ligne où il y a l'erreur
{
m_arme = new Arme(nomArme, degatsArme);
}
Personnage::~Personnage()
{
delete m_arme;
}
void Personnage::recevoirDegats(int nbDegats)
{
m_vie -= nbDegats;
if(m_vie < 0)
{
m_vie = 0;
}
}
void Personnage::attaquer(Personnage &cible)
{
cible.recevoirDegats(m_arme->getDegats());
}
void Personnage::boirePotionDeVie(int quantitePotion)
{
m_vie += quantitePotion;
if(m_vie > 100)
{
m_vie = 100;
}
}
void Personnage::changerArme(string nomNouvelleArme, int degatsNouvelleArme)
{
m_arme->changer(nomNouvelleArme, degatsNouvelleArme);
}
bool Personnage::estVivant()
{
if(m_vie > 0)
{
return true;
}
else
{
return false;
}
}
void Personnage::afficherEtat()
{
cout << "Vie : " << m_vie << endl;
cout << "Mana : " << m_mana << endl;
m_arme->afficher();
}
Arme.h
Code : C++#ifndef DEF_ARME
#define DEF_ARME
#include <iostream>
#include <string>
class Arme
{
public:
Arme();
Arme(std::string nom, int degats);
void changer(std::string nom, int degats);
void afficher();
int Arme::getDegats() const;
private:
std::string m_nom;
int m_degats;
};
#endif
Arme.cpp
Code : C++#include <iostream>
#include <string>
#include "Arme.h"
using namespace std;
Arme::Arme() : m_nom("Epée mouillée"), m_degats(10)
{
}
Arme::Arme(string nom, int degats) : m_nom(nom), m_degats(degats)
{
}
void Arme::changer(string nom, int degats)
{
m_nom = nom;
m_degats = degats;
}
void Arme::afficher()
{
cout << m_nom << " (Dégats : " << m_degats << " )" << endl;
}
int Arme::getDegats() const
{
return m_degats;
}
main.c
Code : C++#include <iostream>
#include <string>
#include "Personnage.h"
using namespace std;
int main()
{
// Création des personnages
Personnage david, goliath("Epee aiguisee", 20);
// Au combat !
goliath.attaquer(david);
david.boirePotionDeVie(20);
goliath.attaquer(david);
david.attaquer(goliath);
goliath.changerArme("Double hache tranchante veneneuse de la mort", 40);
goliath.attaquer(david);
// Temps mort ! Voyons voir la vie de chacun...
cout << "David" << endl;
david.afficherEtat();
cout << endl << "Goliath" << endl;
goliath.afficherEtat();
system("PAUSE");
return 0;
}
Bon comme je vous l'ai dis je ne comprend pas l'erreur donc je met tout le code
En tout cas merci de m'éclairer car je ne vois pas vraiment comment tout ça se peut.
Merci de votre aide!