Partage
  • Partager sur Facebook
  • Partager sur Twitter

Methode qui retourne un structure

return struct[7];

Sujet résolu
    15 juin 2007 à 18:19:18

    Bonjour,

    Je viens de finir le charpitre 7 de la partie IV, mais j'ai un problème quand je continue le petit RPG...

    En effet, j'ai créé un structure SpellInfo:
        struct SpellInfo
        {
            std::string m_name;
            long m_damage;
            long m_mana;
            long m_type;
        };


    je ré utilise cette structure dans dans ma class 'spell'. Mais ma methode 'useSpell' ce trouve elle dans un autre class (Personnage). Alors voila, je voudrais pouvoir récuperer les infos stoquées dans un tableau 'spellPlayer' de type 'SpellInfo' via un methode 'getSpell'. Mais comme ce que je veux récuperer est un structure, je ne peux pas le retourner de la manière simple:
    SpellInfo Spell::getSpell(int spellNumber) const;

    Le compilateur me retourne une erreur car je ne peux pas retourner un SpellInfo... (le 'in spellNumber, c'est par ce que je veux pourvoir stocker plusieur pouvoirs, donc j'ai fait un tableau: SpellInfo spellPlayer[10]; )

    Comment puis-je récuperer mes infos sur les pourvoirs de mes personnages?


    Citation : class Spell

    #ifndef DEF_SPELL
    #define DEF_SPELL

    #define NORMAL 0
    #define ICE 1
    #define WATER 2
    #define FIRE 3




    class Spell
    {
        public:

        Spell();
        void newSpell(std::string name, long damage, long mana, long type, long spellNumber);
        void Spell::getSpell(int spellNumber) const;

        private:

        struct SpellInfo
        {
            std::string m_name;
            long m_damage;
            long m_mana;
            long m_type;
        };
        SpellInfo spellPlayer[10];
    };
    #endif



    Serait-il mieux de mettre ma structure en dehore de ma class?

    Citation : spell.cpp

    #include <string>
    #include <iostream>
    #include "spell.h"
    #include "personnage.h"

    using namespace std;

    //Constructeurs
    Spell::Spell()
    {
        for (int i = 0; i < 10; i++)
        {
            spellPlayer[i].m_name = "";
            spellPlayer[i].m_damage = 0;
            spellPlayer[i].m_mana = 0;
            spellPlayer[i].m_type = 0;
        }
    }

    //Methodes
    void Spell::newSpell(string name, long damage, long mana, long type, long spellNumber)
    {
        spellPlayer[spellNumber].m_name = name;
        spellPlayer[spellNumber].m_damage = damage;
        spellPlayer[spellNumber].m_mana = mana;
        spellPlayer[spellNumber].m_type = type;
    }

    void Spell::getSpell(int spellNumber) const
    {

    }


    Citation : Personnage.h (class Personnage)

    #ifndef DEF_PERSONNAGE
    #define DEF_PERSONNAGE

    #include "Arme.h"
    #include "spell.h"

    class Personnage
    {
        public:

        Personnage(std::string nom);
        Personnage(std::string nom, std::string nomArme, int degatsArme);
        Personnage(std::string nom, int vie, int mana, std::string nomArme, int degatsArme);
        int recevoirDegats(int nbDegats, long type = NORMAL);
        void attaquer(Personnage &cible);
        void boirePotionDeVie(int quantitePotion, std::string potion);
        void Personnage::boirePotionDeMana(int quantitePotion, std::string potion);
        void changerArme(std::string nomNouvelleArme, int degatsNouvelleArme);
        void useSpell (long spellNumber, Personnage &cible); /*C'est ici que je dois utiliser mon getSpell*/
        bool estVivant();
        void afficherEtat();
        int voirDegats(int nbDegats);
        int getMana() const;
        int getLife() const;
        int getNbManaPot() const;
        int getNbLifePot() const;

        private:

        int m_vie;
        int m_mana;
        int m_nbManaPot;
        int m_nbLifePot;
        Arme m_arme;
        Spell m_spell;
        std::string m_name;
    };

    #endif




    Citation : Personnage.cpp

    #include <string>
    #include <iostream>
    #include "Personnage.h"
    #include "spell.h"

    using namespace std;

    //Constructeurs
    Personnage::Personnage(std::string nom) : m_name(nom), m_vie(100), m_mana (100), m_arme("No weapon", 0), m_nbLifePot(0), m_nbManaPot(0) {}
    Personnage::Personnage(std::string nom, std::string nomArme, int degatsArme) : m_name(nom), m_vie(100), m_mana (100), m_arme(nomArme, degatsArme), m_nbLifePot(1), m_nbManaPot(1) {}
    Personnage::Personnage(std::string nom, int vie, int mana, std::string nomArme, int degatsArme) : m_name(nom), m_vie(vie), m_mana(mana), m_arme(nomArme, degatsArme), m_nbLifePot(1), m_nbManaPot(1) {}

    //Methodes
    int Personnage::recevoirDegats(int nbDegats, long type)
    {
        m_vie -= nbDegats;

        if (m_vie < 0)
        {
            m_vie = 0;
        }

        return nbDegats;
    }

    int Personnage::voirDegats(int nbDegats)
    {
        return nbDegats;
    }

    void Personnage::attaquer(Personnage &cible)
    {
        cible.recevoirDegats(m_arme.getDegats());
        cout << m_name << " attacks " << cible.m_name << " with ";
        m_arme.afficher();
        cout << cible.m_name << " looses " << cible.voirDegats(m_arme.getDegats()) << " hit points (" << cible.m_vie << "/100 HP)" << endl;

    }


    void Personnage::useSpell(long spellNumber, Personnage &cible)
    {
        /*C'est ici que je dois utiliser mon getSpell*/
    }


    void Personnage::boirePotionDeVie(int quantitePotion, string potion)
    {
        m_vie += quantitePotion;

        if (m_vie > 100)
        {
            m_vie = 100;
        }

        m_nbLifePot--;

        cout << m_name << " drinks " << potion << " and gain " << quantitePotion << " hit points (" << m_vie << "/100 HP)" << endl;
        cout << "Number of life potions that " << m_name << " has : " << m_nbLifePot << endl;
    }

    void Personnage::boirePotionDeMana(int quantitePotion, string potion)
    {
        m_mana += quantitePotion;

        if (m_mana > 100)
        {
            m_mana = 100;
        }

        m_nbManaPot--;

        cout << "Number of mana potions that " << m_name << " has : " << m_nbManaPot << endl;
        cout << m_name << " drinks " << potion << " and gain " << quantitePotion << " of mana (" << m_mana << "/100)endl" << endl;
    }

    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 << m_name << " : " << endl;
        cout << "HP : " << m_vie << endl;
        cout << "Mana : " << m_mana << endl;
        cout << "Number of life potions : " << m_nbLifePot << endl;
        cout << "Number of mana potions : " <<  m_nbManaPot << endl;
        cout << "Weapon : ";
        m_arme.afficher();
        cout << endl;
    }

    int Personnage::getMana() const
    {
        return m_mana;
    }

    int Personnage::getNbManaPot() const
    {
        return m_nbManaPot;
    }

    int Personnage::getLife() const
    {
        return m_vie;
    }

    int Personnage::getNbLifePot() const
    {
        return m_nbLifePot;
    }




    La question est donc, comment puis-je récuperer mes infos sur le sort utilisé?

    Merci d'avance ;)

    EDIT:
    P.S. Si les commentaires sont en français, en anglais, de même pour les variables, c'est pas ce que je vais continuer ce petit RPG avec un ami qui ne parle qu'anglais ;)
    • Partager sur Facebook
    • Partager sur Twitter
      15 juin 2007 à 22:11:08

      Déjà, si tu veux que ta fonction retourne une structure de type spellInfo, faudrais que ta fonction sois de type spellInfo et non void.
      • Partager sur Facebook
      • Partager sur Twitter
        16 juin 2007 à 6:13:30

        Je sais, mais c'est ce que j'ai dis au début, ça ne retourne rien...
        Il y a une erreur à la compilation:
        Spell Info does not name a type.

        Ha! Si, j'ai trouvé! Je suis vraiment c**... Bein sur que j'ai cette erreur ma structure est déclaré après... Mon prototype!

        Ben merci de cette réponse (mine de rien, c'était la solution)!
        • Partager sur Facebook
        • Partager sur Twitter

        Methode qui retourne un structure

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown