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

Langage C++ Discussion :

Creer un objet " personnage" dans un tableau de personnage


Sujet :

Langage C++

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut Creer un objet " personnage" dans un tableau de personnage
    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:

    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.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
     
    #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();
     
    }
    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
     
    #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.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
     
    #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;
    }
    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
     
    #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

  2. #2
    Rédacteur/Modérateur
    Avatar de JolyLoic
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    5 463
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 5 463
    Points : 16 213
    Points
    16 213
    Par défaut
    Dès que tu as écrit : Personnage joueurs[nombreDeJoueurs];, tu as déjà créé tous les personnages de ton tableau (avec le constructeur par défaut). Si tu veux créer les personnages au fur et à mesure, il vaut mieux utiliser une autre manière de gérer les tableaux, plus puissante : vector.

    Ton code ressemblerait alors à :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    vector<Personnage> joueurs; // Là, le tableau est vide
     
    for(int i(0); i < nombreDeJoueurs; i++)
    {
        // tu poses tes questions
        joueurs.push_back(Personnage(nomJoueur,"epee",20,20));
    }
    Pour que ça marche, il faut que ta classe Personnage soit copiable, ce qui est ton cas. Il y a des moyens d'éviter ça, mais je préfère ne pas t'embrouiller pour l'instant.

    Juste un point : Le destructeur que tu as écrit ne sert à rien, le compilateur aurait très bien pu l'écrire à ta place. Et c'est même pire : Sa seule présence va empêcher des optimisations importantes. Donc un conseil : Vire le.
    Ma session aux Microsoft TechDays 2013 : Développer en natif avec C++11.
    Celle des Microsoft TechDays 2014 : Bonnes pratiques pour apprivoiser le C++11 avec Visual C++
    Et celle des Microsoft TechDays 2015 : Visual C++ 2015 : voyage à la découverte d'un nouveau monde
    Je donne des formations au C++ en entreprise, n'hésitez pas à me contacter.

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Je me doutait bien qu'il faudrait passer avec des vector, je ne regarde pas ton code pour essayer de trouver tout seul! (oui c'est ma methode de travail ^^)
    Merci de m'avoir mis sur la bonne voie

Discussions similaires

  1. Réponses: 2
    Dernier message: 02/06/2006, 12h59

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