Partage
  • Partager sur Facebook
  • Partager sur Twitter

Erreur entre deux class

    26 octobre 2018 à 11:27:15

    Bonjour, j'ai une erreur que je n'arrive pas à résoudre.

    Voici le code : 

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    class Adherent{
    string nomAdh;
    string prenomAdh;
    bool aCotise;
    
    public:
        Adherent();
        Adherent(string nom, string prenom, bool cotise);
        Adherent(const Adherent & adh);
        void setnomAdh(string nom);
        void setprenomAdh(string prenom);
        void setaCotise(bool cotise);
        string getnomAdh();
        string getprenomAdh();
        bool getaCotise();
        void affichage();
    };
    
    class Asso : public Adherent{
    string nomAsso;
    vector<Adherent*>LesAdherents;
    
    public:
        void afficherLesAdh();
    };
    
    void Asso::afficherLesAdh(){
        cout<<LesAdherents[0]<<endl;
    }
    
    
    Adherent::Adherent(){
        nomAdh = "unknow";
        prenomAdh = "unknow";
        aCotise = 0;
    }
    Adherent::Adherent(string nom, string prenom, bool cotise){
        nomAdh = nom;
        prenomAdh = prenom;
        aCotise = cotise;
    }
    Adherent::Adherent(const Adherent & adh){
        nomAdh = adh.nomAdh;
        prenomAdh = adh.prenomAdh;
        aCotise = adh.aCotise;
    };
    
    void Adherent::setnomAdh(string nom){
        nomAdh = nom;
    }
    void Adherent::setprenomAdh(string prenom){
        prenomAdh = prenom;
    }
    void Adherent::setaCotise(bool cotise){
        aCotise = cotise;
    }
    string Adherent::getnomAdh(){
        return nomAdh;
    }
    string Adherent::getprenomAdh(){
        return prenomAdh;
    }
    bool Adherent::getaCotise(){
        return aCotise;
    }
    void Adherent::affichage(){
         string var;
        if(aCotise == true){
            var = "oui";
        }else{
            var = "non";
        }
        cout<<"Nom :             "<<nomAdh<<" \nPrenom :          "<<prenomAdh<<"\nCotisation recu : "<<var<<"\n"<<endl;
    }
    void lirefichier(Adherent *adh[100]){
        int adhadd = 0;
        int variable=1;
        ifstream fichier("fichier.txt");
        string uneLigne;
        if (! fichier.is_open())
            cerr<<"impossible d'ouvrir le fichier "<<endl;
        else
        {
        while (fichier >> uneLigne)
            if(variable == 1){
                adh[adhadd] = new Adherent;
                adh[adhadd]->setnomAdh(uneLigne);
                variable++;
            }else if(variable == 2){
                adh[adhadd]->setprenomAdh(uneLigne);
                variable++;
            }else if(variable ==3){
                bool testbool;
                if(uneLigne == "1"){
                    testbool = true;
                }else{
                    testbool = false;
                }
                adh[adhadd]->setaCotise(testbool);
                variable = 1;
                adhadd++;
            }
            fichier.close();
        }
    }
    
    int main()
    {
        Adherent *adh[10];
        lirefichier(adh);
    //    int i;
    //    for(i=0; i<=9; i++){
    //        adh[i]->affichage();
    //        cout<<"--------------------------------\n"<<endl;
    //    }
        adh[0]->afficherLesAdh();
        return 0;
    }

    Voici l'erreur : 

    123|error: 'class Adherent' has no member named 'afficherLesAdh'|

    • Partager sur Facebook
    • Partager sur Twitter
      26 octobre 2018 à 11:39:35

      Hola.

      Ctrl+F --> écrit "afficherLesAdh" --> Tout surligner


      Tu remarqueras que ta fonction afficherLesAdh() que tu appelles ligne 123 n'existe pas dans ta classe Adherent.

      Tu l'as seulement défini pour ta class Asso.

      -
      Edité par LilyKianii 26 octobre 2018 à 11:39:56

      • Partager sur Facebook
      • Partager sur Twitter
        26 octobre 2018 à 11:47:06

        Hello,

        Ton code est hyper mega degeulasse (desole).

        Pourquoi asso herite de adherent? Pourquoi un tableau de pointeur?

        Pourquoi des fonctions avec des tableaux a la C? Pourquoi ce naming inconsistant? Pourquoi ces indentations degeulasse?

        Pourquoi tout ces setter?

        Repond a toutes ces questions, et tu pourras mieux coder.

        Sinon, niveau naming utilise le meme que le standard, c'est plus simple de s'y retrouver (Meme si des lib comme Qt en ont rien eu a faire)

        • Partager sur Facebook
        • Partager sur Twitter

        Architecte logiciel - Software craftsmanship convaincu.

          26 octobre 2018 à 13:35:20

          Un premier élément pour aider à répondre:

          L'héritage public définit une relation "est-un" entre la classe de base et la classe héritante.
          En d'autres termes, la classe héritante est une classe de base.

          • Partager sur Facebook
          • Partager sur Twitter
            26 octobre 2018 à 14:58:37

            Salut, dans ta classe Adhérent tu définis une fonction nommée Affichage (ligne 22). C'est peut être juste ça, non ?
            • Partager sur Facebook
            • Partager sur Twitter

            C++ à la vie à la mort. (Le python c'est bien aussi) || Rejoignez : https://discord.gg/9r3zqgg

              26 octobre 2018 à 15:03:43

              RE

              Merci pour vos réponses, je n'est pas encore résolu le problème mais cela avance je peux mtn accéder dans ma procédure afficherLesAdh mais a l'affichage ca ne passe pas, ca me retourne une erreur lors du passe du cout avec la variable LesAdherents[0] par contre le TEST juste avant s'affiche correctement.

              Le nouveau code :

              #include <iostream>
              #include <string>
              #include <fstream>
              #include <vector>
              
              using namespace std;
              
              class Adherent{
              string nomAdh;
              string prenomAdh;
              bool aCotise;
              
              public:
                  Adherent();
                  Adherent(string nom, string prenom, bool cotise);
                  Adherent(const Adherent & adh);
                  void setnomAdh(string nom);
                  void setprenomAdh(string prenom);
                  void setaCotise(bool cotise);
                  string getnomAdh();
                  string getprenomAdh();
                  bool getaCotise();
                  void affichage();
              };
              
              class Asso : public Adherent{
              string nomAsso;
              vector<Adherent*>LesAdherents;
              
              public:
                  void afficherLesAdh();
              };
              
              void Asso::afficherLesAdh(){
                  cout<<"test";
                  cout<<LesAdherents[0]<<endl;
              }
              
              Adherent::Adherent(){
                  nomAdh = "unknow";
                  prenomAdh = "unknow";
                  aCotise = 0;
              }
              Adherent::Adherent(string nom, string prenom, bool cotise){
                  nomAdh = nom;
                  prenomAdh = prenom;
                  aCotise = cotise;
              }
              Adherent::Adherent(const Adherent & adh){
                  nomAdh = adh.nomAdh;
                  prenomAdh = adh.prenomAdh;
                  aCotise = adh.aCotise;
              };
              
              void Adherent::setnomAdh(string nom){
                  nomAdh = nom;
              }
              void Adherent::setprenomAdh(string prenom){
                  prenomAdh = prenom;
              }
              void Adherent::setaCotise(bool cotise){
                  aCotise = cotise;
              }
              string Adherent::getnomAdh(){
                  return nomAdh;
              }
              string Adherent::getprenomAdh(){
                  return prenomAdh;
              }
              bool Adherent::getaCotise(){
                  return aCotise;
              }
              void Adherent::affichage(){
                   string var;
                  if(aCotise == true){
                      var = "oui";
                  }else{
                      var = "non";
                  }
                  cout<<"Nom :             "<<nomAdh<<" \nPrenom :          "<<prenomAdh<<"\nCotisation recu : "<<var<<"\n"<<endl;
              }
              void lirefichier(Asso *adh[10]){
                  int adhadd = 0;
                  int variable=1;
                  ifstream fichier("fichier.txt");
                  string uneLigne;
                  if (! fichier.is_open())
                      cerr<<"impossible d'ouvrir le fichier "<<endl;
                  else
                  {
                  while (fichier >> uneLigne)
                      if(variable == 1){
                          adh[adhadd] = new Asso;
                          adh[adhadd]->setnomAdh(uneLigne);
                          variable++;
                      }else if(variable == 2){
                          adh[adhadd]->setprenomAdh(uneLigne);
                          variable++;
                      }else if(variable ==3){
                          bool testbool;
                          if(uneLigne == "1"){
                              testbool = true;
                          }else{
                              testbool = false;
                          }
                          adh[adhadd]->setaCotise(testbool);
                          variable = 1;
                          adhadd++;
                      }
                      fichier.close();
                  }
              }
              
              int main()
              {
                  Asso *adh[10];
                  lirefichier(adh);
                  adh[0]->afficherLesAdh();
                  return 0;
              }



              • Partager sur Facebook
              • Partager sur Twitter
                26 octobre 2018 à 15:45:42

                Repond deja aux questions.

                On t'aidera a comprendre, pas a te le faire ;)

                Car la ton code reste crado.

                -
                Edité par necros211 26 octobre 2018 à 15:46:09

                • Partager sur Facebook
                • Partager sur Twitter

                Architecte logiciel - Software craftsmanship convaincu.

                Erreur entre deux class

                × 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