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 :

template list pair map


Sujet :

Langage C++

  1. #1
    Membre habitué
    Inscrit en
    Décembre 2007
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 11
    Par défaut template list pair map
    bonjour,
    je narrive pas a savoir pk mon prog plan avec plein de message d'erreur sur les pair map , alors ke je veux tt simplement faire une list de pair contenant un string et un arbre

    Code C++ : 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
    ifndef ENV_HH                                                                                                                                                                                                   # define ENV_HH                                                                                                                                                                                                  #include <string>                                                                                                                                                                                                #include <list>                                                                                                                                                                                                  
    #include <map>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    template <class T>                                                                                                                                                                                               
    class Env                                                                                                                                                                                                        
    {                                                                                                                                                                                                                
      public:                                                                                                                                                                                                        
        //------------------------------                                                                                                                                                                             
        //constructeur/destructeur:                                                                                                                                                                                  
        //------------------------------                                                                                                                                                                             
        Env(){}                                                                                                                                                                                                      
        virtual ~Env(){};                                                                                                                                                                                            
     
        //------------------------------                                                                                                                                                                             
        //Fonctions:                                                                                                                                                                                                 
        //------------------------------                                                                                                                                                                             
        void add_list (std::string s, T& t)                                                                                                                                                                          
        {                                                                                                                                                                                                            
          typename std::map<std::string, T>::iterator i;                                                                                                                                                             
          if (l.begin() == l.end())                                                                                                                                                                                  
            l.push_back(std::pair<std::string, T> (s, t));                                                                                                                                                           
          else                                                                                                                                                                                                       
          {                                                                                                                                                                                                          
            m.insert(l.begin(), l.end());                                                                                                                                                                            
            for (i = m.begin(); i != m.end(); i++)                                                                                                                                                                   
              if (i->first == s)                                                                                                                                                                                     
                i->second = t;                                                                                                                                                                                       
              else                                                                                                                                                                                                   
                l.push_back(std::map<std::string, T> (s, t));                                                                                                                                                        
          }                                                                                                                                                                                                          
        }                                                                                                                                                                                                            
     
        int search_list(std::string s)                                                                                                                                                                               
        {                                                                                                                                                                                                            
          if (l.begin() == l.end() &&  l.begin()->first == s)                                                                                                                                                        
            return (l.begin()->second->value ());                                                                                                                                                                    
          else                                                                                                                                                                                                       
          {                                                                                                                                                                                                          
            m.insert(l.begin(), l.end());                                                                                                                                                                            
            typename std::map<std::string, T>::iterator i = m.begin();                                                                                                                                               
            for ( ; i != m.end(); i++)                                                                                                                                                                               
              if (i->first == s)                                                                                                                                                                                     
                return (i->second->value ());                                                                                                                                                                        
            return (1);                                                                                                                                                                                              
          }                                                                                                                                                                                                          
        }                
     
      protected:                                                                                                                                                                                                     
        //------------------------------                                                                                                                                                                             
        //Variables:                                                                                                                                                                                                 
        //------------------------------                                                                                                                                                                             
        std::list<std::pair<std::string, T> > l;                                                                                                                                                                     
        std::map<std::string, T> m;                                                                                                                                                                                  
     
    };                                                                                                                                                                                                               
     
    //#include "Env.hxx"                                                                                                                                                                                             
     
    #endif
    [Balise CODE ajouté par mongaulois]

  2. #2
    Membre Expert

    Profil pro
    Inscrit en
    Juin 2006
    Messages
    1 294
    Détails du profil
    Informations personnelles :
    Localisation : Royaume-Uni

    Informations forums :
    Inscription : Juin 2006
    Messages : 1 294
    Par défaut
    Salut,

    Si tu ne dis pas où ça plante (je suppose que plan est l'abréviation de plante ?) et quels sont les messages d'erreur, il y a fort peu de chances qu'on le devine.

    Ah et puis parce que c'est complètement illisible là...

    MAT.

  3. #3
    Membre habitué
    Inscrit en
    Décembre 2007
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 11
    Par défaut
    ben en faite euh le code d 'erreur est euh indechiffrable:

    m -f *.o
    g++ -g -Wall -Wextra -Werror -pedantic -c -o minimathique.o minimathique.cc
    /usr/include/c++/3.4/bits/stl_pair.h: In instantiation of `std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>':
    /usr/include/c++/3.4/bits/stl_list.h:100: instantiated from `std::_List_node<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >'
    /usr/include/c++/3.4/bits/list.tcc:72: instantiated from `void std::_List_base<_Tp, _Alloc>::_M_clear() [with _Tp = std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>, _Alloc = std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >]'
    /usr/include/c++/3.4/bits/stl_list.h:328: instantiated from `std::_List_base<_Tp, _Alloc>::~_List_base() [with _Tp = std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>, _Alloc = std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >]'
    lexer-parser/../env/Env.hh:15: instantiated from `Env<T>::Env() [with T = EvalTree]'
    minimathique.cc:10: instantiated from here
    /usr/include/c++/3.4/bits/stl_pair.h:69: error: cannot declare field `std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>::second' to be of type `EvalTree'
    /usr/include/c++/3.4/bits/stl_pair.h:69: error: because the following virtual functions are abstract:
    lexer-parser/../opbin/../EvalTree.hh:23: error: virtual std::string EvalTree::values() const
    lexer-parser/../opbin/../EvalTree.hh:22: error: virtual int EvalTree::value() const
    lexer-parser/../opbin/../EvalTree.hh:21: error: virtual void EvalTree::print() const
    /usr/include/c++/3.4/bits/stl_pair.h: In instantiation of `std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>':
    /usr/include/c++/3.4/bits/stl_tree.h:135: instantiated from `std::_Rb_tree_node<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >'
    /usr/include/c++/3.4/bits/stl_tree.h:1064: instantiated from `void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >]'
    /usr/include/c++/3.4/bits/stl_tree.h:565: instantiated from `std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree> >]'
    lexer-parser/../env/Env.hh:15: instantiated from `Env<T>::Env() [with T = EvalTree]'
    minimathique.cc:10: instantiated from here
    /usr/include/c++/3.4/bits/stl_pair.h:69: error: cannot declare field `std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>::second' to be of type `EvalTree'
    /usr/include/c++/3.4/bits/stl_pair.h:69: error: since type `EvalTree' has abstract virtual functions
    gmake: *** [minimathique.o] Error 1

  4. #4
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 035
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 035
    Par défaut
    Bonjour,
    As tu implementé l'operator = et le constructeur par recopie dans ta class EvalTree
    ???

    Sinon pense :
    *
    * std::map<std::string, T> (s, t) peut etre remplacé par make_pair(s,t)
    * En regardant ton code , une map semble mieux. Surtout pour les insert et le search. Déja implementé et trés rapide

  5. #5
    Membre habitué
    Inscrit en
    Décembre 2007
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 11
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Bonjour,
    As tu implementé l'operator = et le constructeur par recopie dans ta class EvalTree
    ???
    euh non je lai pa implemente et pk?
    c koi make_pair?

  6. #6
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 035
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 035
    Par défaut
    Citation Envoyé par blackfiever Voir le message
    euh non je lai pa implemente et pk?
    Pendant le push_back et la creation du pair, ce sont ces operateurs qui sont appelés pour l'ajouter dans le contenaire

    Citation Envoyé par blackfiever Voir le message
    c koi make_pair?
    la même chose que std::pair<std::string, T> (s, t)
    mais c'est plus rapide as ecrire
    Code C++ : Sélectionner tout - Visualiser dans une fenêtre à part
    std::pair<std::string, T> (s, t) <=> make_pair(s,t);

  7. #7
    Nouveau candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 2
    Par défaut
    D'après l'erreur du compilateur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    /usr/include/c++/3.4/bits/stl_pair.h:69: error: cannot declare field `std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, EvalTree>::second' to be of type `EvalTree'
    /usr/include/c++/3.4/bits/stl_pair.h:69: error: because the following virtual functions are abstract:
    lexer-parser/../opbin/../EvalTree.hh:23: error: virtual std::string EvalTree::values() const
    lexer-parser/../opbin/../EvalTree.hh:22: error: virtual int EvalTree::value() const
    lexer-parser/../opbin/../EvalTree.hh:21: error: virtual void EvalTree::print() const
    il semblerait que tu essais d'instancier un EvalTree qui serait une classe abstraite.

Discussions similaires

  1. JDK 7: Proposition 9 : Notation de tableau pour List et Map -> Intégrée
    Par vbrabant dans le forum Collection et Stream
    Réponses: 58
    Dernier message: 03/09/2009, 15h35
  2. Problème de class template et std::map
    Par bathof dans le forum Langage
    Réponses: 2
    Dernier message: 31/07/2007, 22h18
  3. Pb avec template list
    Par olive_le_malin dans le forum Langage
    Réponses: 10
    Dernier message: 16/11/2006, 02h05
  4. Utilisation d'un template avec une map
    Par bonano dans le forum Langage
    Réponses: 6
    Dernier message: 09/08/2006, 21h26
  5. Fuite de mémoire en utilisant le template list
    Par schtroumpf_farceur dans le forum Langage
    Réponses: 9
    Dernier message: 18/07/2005, 20h44

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