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 :

probleme template hash_map


Sujet :

Langage C++

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2
    Points : 2
    Points
    2
    Par défaut probleme template hash_map
    Bonjour

    J'aurais besoin d'aide pour utiliser le template hash_map de
    la STL (extensions GNU sous Linux), car j'ai des bugs de
    compilation que je ne parviens pas à résoudre.

    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
     
    struct pair {
        char* nom;
        char* definition;
    };
     
    pair dico[] = {
        { "alumette", "instrument pour allumer du feu" },
        { "cheval", "quadrupède servant à la monte" },
        { "bateau", "moyen de transport aquatique" },
        { "carte", "bout de papier" },
        { "doigt", "extrémité de la main" },
        { "essai", "application d'un acte dont on ignore s'il peut réussir" },
        { "gratuit", "qu'il ne faut pas payer" },
        { "histoire", "récit d'événements réels ou fictifs" },
    };
     
    struct eqstr
    {
        bool operator()(char *s1, char *s2)
        {
            return strcmp(s1, s2) == 0;
        }
    };
     
    int main(int argc, char **argv)
    {
        __gnu_cxx::hash_map<char*, char*, __gnu_cxx::hash<char*>, eqstr> h;
        int i;
     
        for (i=0; i<=7; i++) {
            h.insert(dico[i].nom, dico[i].definition);
        }
     
        cout << h.find("essai")->second << endl;
     
        return 0;
    }
    Résultat de la compilation :

    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
     
    $ g++ -o test test.cpp
    /usr/include/c++/3.3/ext/stl_hashtable.h: Dans member function « void
       __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey,
       _Alloc>::insert_unique(_ForwardIterator, _ForwardIterator,
       std::forward_iterator_tag) [with _ForwardIterator = char*, _Val =
       std::pair<char* const, char*>, _Key = char*, _HashFcn =
       __gnu_cxx::hash<char*>, _ExtractKey = std::_Select1st<std::pair<char* const,
       char*> >, _EqualKey = eqstr, _Alloc = std::allocator<char*>] »:
    /usr/include/c++/3.3/ext/stl_hashtable.h:394:   instantiated from `void __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::insert_unique(_InputIterator, _InputIterator) [with _InputIterator = char*, _Val = std::pair<char* const, char*>, _Key = char*, _HashFcn = __gnu_cxx::hash<char*>, _ExtractKey = std::_Select1st<std::pair<char* const, char*> >, _EqualKey = eqstr, _Alloc = std::allocator<char*>]'
    /usr/include/c++/3.3/ext/hash_map:174:   instantiated from `void __gnu_cxx::hash_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = char*, _Key = char*, _Tp = char*, _HashFcn = __gnu_cxx::hash<char*>, _EqualKey = eqstr, _Alloc = std::allocator<char*>]'
    test.cpp:37:   instantiated from here
    /usr/include/c++/3.3/ext/stl_hashtable.h:426: error: no matching function for
       call to `__gnu_cxx::hashtable<std::pair<char* const, char*>, char*,
       __gnu_cxx::hash<char*>, std::_Select1st<std::pair<char* const, char*> >,
       eqstr, std::allocator<char*> >::insert_unique_noresize(char&)'
    /usr/include/c++/3.3/ext/stl_hashtable.h:654: error: candidates are:
       std::pair<__gnu_cxx::_Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey,
       _EqualKey, _Alloc>, bool> __gnu_cxx::hashtable<_Val, _Key, _HashFcn,
       _ExtractKey, _EqualKey, _Alloc>::insert_unique_noresize(const _Val&) [with
       _Val = std::pair<char* const, char*>, _Key = char*, _HashFcn =
       __gnu_cxx::hash<char*>, _ExtractKey = std::_Select1st<std::pair<char* const,
       char*> >, _EqualKey = eqstr, _Alloc = std::allocator<char*>]

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    258
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Bas Rhin (Alsace)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 258
    Points : 307
    Points
    307
    Par défaut
    La fonction membre insert ne s'utilise pas de cette façon : elle prend en paramètre soit une std::pair<Key, Data> soit un couple d'itérateurs sur des paires pour insérer une séquence. Tu peux aussi utiliser operator[] pour l'insertion. Il y a un exemple là : http://www.sgi.com/tech/stl/hash_map.html

Discussions similaires

  1. probleme template a plusieurs parametres
    Par typedef dans le forum Langage
    Réponses: 4
    Dernier message: 01/05/2010, 08h13
  2. [probleme] template singleton et .lib static
    Par typedef dans le forum Bibliothèques
    Réponses: 12
    Dernier message: 09/02/2010, 16h21
  3. probleme template qcm en loadmovie
    Par serna dans le forum Flash
    Réponses: 3
    Dernier message: 05/06/2006, 22h59
  4. Probleme Template
    Par Muetdhiver dans le forum Langage
    Réponses: 11
    Dernier message: 18/04/2006, 11h00
  5. probleme template me renvoi qq chose en trop
    Par Triangle dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 29/07/2005, 15h01

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