Bonjour, je m'adresse a vous aujourd'hui car j'ai un soucis dans mon code, je voudrais créer des personnages en passant par un tableau de personnage (et une boucle "for"). Mais j'ai une erreur que je n'arrive pas à comprendre!
L'erreur se trouve à la ligne 29 de main.cpp
L'erreur: error: no match for call to '(Personnage) (std::string&, std::string, int, int)'
Je vais vous donner le code des fichiers de mon projet:
main.cpp:
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 #include <iostream> #include "Personnage.h" using namespace std; int main() { int nombreDeJoueurs; string nomJoueur; string nom; string nomCibles; cout << "Combien de joueurs êtes vous?: " << endl; cin >> nombreDeJoueurs; Personnage joueurs[nombreDeJoueurs]; for(int i(0); i < nombreDeJoueurs; i++) { int classe; cout << "Nom du joueurs " << i+1 << ": "; cin >> nomJoueur; cout << endl << "Classe:" << endl << "1. Mage" << endl << "2. Guerrier" << endl; cin >> classe; joueurs[i](nomJoueur,string("epee"),20,20); // ERREUR!!!!! } for(int i(0); i < nombreDeJoueurs; i++) { int cible; int choix; nom = joueurs[i].getNom(); cout << "Tour de " << nom << endl; cout << "1. Attaquer" << endl; cout << "2. Boire une potion" << endl; cin >> choix; if(choix==1) { cout << "Cibles: " << endl; for(int x(0); x < nombreDeJoueurs; x++) { if(i!=x) { nomCibles = joueurs[x].getNom(); cout << x << ". " << nomCibles << endl; } } cin >> cible; joueurs[i].attaquer(joueurs[cible]); } else { joueurs[i].boirePotionDeVie(20); } } return 0; }
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
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 #include "Personnage.h" #include <string> using namespace std; Personnage::Personnage(string nom) : m_vie(100), m_mana(100),m_nom(nom) { } Personnage::Personnage(string nom, string nomArme, int degatsArme, int manaArme) : m_vie(100), m_mana(100),m_nom(nom),m_arme(nomArme, degatsArme, manaArme) { } Personnage::~Personnage() { } void Personnage::recevoirDegats(int nbDegats) { m_vie -= nbDegats; //On enlève le nombre de dégâts reçus à la vie du personnage if (m_vie < 0) //Pour éviter d'avoir une vie négative { m_vie = 0; //On met la vie à 0 (cela veut dire mort) } cout << endl << "Aille! " << m_nom << " à reçu: " << nbDegats << " dégâts" << endl; } std::string Personnage::getNom() { return m_nom; } void Personnage::attaquer(Personnage &cible) { std::string nomDeVictime = cible.getNom(); int degats = m_arme.getDegats(); m_mana -= m_arme.getMana(); cout << endl << m_nom << " inflige " << degats << " à " << nomDeVictime << "!" << endl; cout << endl << "Cout en mana: " << m_arme.getMana() << "! Il vous reste: " << m_mana << " points de mana!" << endl; cible.recevoirDegats(degats); //On inflige à la cible les dégâts que cause notre arme } void Personnage::boirePotionDeVie(int quantitePotion) { m_vie += quantitePotion; if (m_vie > 100) { m_vie = 100; } cout << endl << m_nom << " boit une potion de vie qui le regenère de: " << quantitePotion << " points de vie!" << endl; cout << endl << m_nom << " à maintenant: " << m_vie << " points de vie!" << endl; } void Personnage::changerArme(string nomNouvelleArme, int degatsNouvelleArme, int manaNouvelleArme) { m_arme.changer(nomNouvelleArme,degatsNouvelleArme, manaNouvelleArme); cout << endl << m_nom << " change d'arme! Il se bat maintenant avec: " << nomNouvelleArme << " (" << degatsNouvelleArme << " dégâts et " << m_arme.getMana() << " par attaques) " << endl; } bool Personnage::estVivant() const { return m_vie > 0; } void Personnage::afficherEtat() const { cout << endl << "NOM : " << m_nom << endl; cout << "Vie : " << m_vie << endl; cout << "Mana : " << m_mana << endl; m_arme.afficher(); }
Arme.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 #ifndef DEF_PERSONNAGE #define DEF_PERSONNAGE #include <iostream> #include <string> #include "Arme.h" class Personnage { public: Personnage(std::string nom = "Joueur"); Personnage(std::string nom, std::string nomArme, int degatsArme, int manaArme = 0); ~Personnage(); void recevoirDegats(int nbDegats); void attaquer(Personnage &cible); void boirePotionDeVie(int quantitePotion); void changerArme(std::string nomNouvelleArme, int degatsNouvelleArme, int manaNouvelleArme = 0); bool estVivant() const; void afficherEtat() const; std::string getNom(); private: std::string m_nom; int m_vie; int m_mana; Arme m_arme; }; #endif
Arme.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 #include "Arme.h" #include <string> #include <iostream> using namespace std; Arme::Arme() : m_nom("Épée rouillée"), m_degats(10), m_mana(0) { } Arme::Arme(string nom, int degats, int mana) : m_nom(nom), m_degats(degats), m_mana(mana) { } void Arme::changer(std::string nom, int degats, int mana) { m_nom = nom; m_degats = degats; m_mana = mana; } void Arme::afficher() const { cout << "Arme : " << m_nom << " (Dégâts : " << m_degats << ")" << endl; cout << "Cout en mana par attaque: " << m_mana << endl; } int Arme::getDegats() const { return m_degats; } int Arme::getMana() const { return m_mana; }
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 #ifndef ARME_H_INCLUDED #define ARME_H_INCLUDED #include <string> class Arme { public: Arme(); Arme(std::string nom, int degats, int mana); void changer(std::string nom, int degats, int mana = 0); void afficher() const; int getDegats() const; int getMana() const; private: std::string m_nom; int m_degats; int m_mana; }; #endif // ARME_H_INCLUDED
Partager