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

C++ Discussion :

Probleme de auto_ptr avec les map


Sujet :

C++

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2005
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 15
    Points : 14
    Points
    14
    Par défaut Probleme de auto_ptr avec les map
    Bonjour,

    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
     
    #include <iostream>
    #include <string>
    #include <map>
    #include <memory>
     
    using namespace std;
     
    class Toto
    {
      public:
        Toto(int a){/* nothing*/}
     
        void pouet(void){cout << "coucou" << endl;}
    };
     
    int main(void)
    {
      map<string, auto_ptr<Toto> > tyty;
     
      tyty["ee"].reset(new Toto(4));
      tyty["ee"] -> pouet();
     
      return 0;
    }
    et l'erreur de compilation (sous gcc) :

    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
     
    /usr/include/c++/3.3.3/bits/stl_pair.h: In constructor `std::pair<_T1, 
       _T2>::pair(const _T1&, const _T2&) [with _T1 = const std::string, _T2 = 
       std::auto_ptr<Toto>]':
    /usr/include/c++/3.3.3/bits/stl_map.h:319:   instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp = std::auto_ptr<Toto>, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, std::auto_ptr<Toto> > >]'
    t1.cc:21:   instantiated from here
    /usr/include/c++/3.3.3/bits/stl_pair.h:84: error: passing `const 
       std::auto_ptr<Toto>' as `this' argument of `std::auto_ptr<_Tp>::operator 
       std::auto_ptr_ref<_Tp1>() [with _Tp1 = Toto, _Tp = Toto]' discards 
       qualifiers
    /usr/include/c++/3.3.3/bits/stl_pair.h: In constructor `std::pair<_T1, 
       _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = const std::string, _U2 = 
       std::auto_ptr<Toto>, _T1 = const std::string, _T2 = std::auto_ptr<Toto>]':
    /usr/include/c++/3.3.3/bits/stl_construct.h:78:   instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 = std::pair<const std::string, std::auto_ptr<Toto> >, _T2 = std::pair<const std::string, std::auto_ptr<Toto> >]'
    /usr/include/c++/3.3.3/bits/stl_tree.h:618:   instantiated from `std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(const _Val&) [with _Key = std::string, _Val = std::pair<const std::string, std::auto_ptr<Toto> >, _KeyOfValue = std::_Select1st<std::pair<const std::string, std::auto_ptr<Toto> > >, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, std::auto_ptr<Toto> > >]'
    /usr/include/c++/3.3.3/bits/stl_tree.h:985:   instantiated from `std::_Rb_tree_iterator<_Val, _Val&, _Val*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, const _Val&) [with _Key = std::string, _Val = std::pair<const std::string, std::auto_ptr<Toto> >, _KeyOfValue = std::_Select1st<std::pair<const std::string, std::auto_ptr<Toto> > >, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, std::auto_ptr<Toto> > >]'
    /usr/include/c++/3.3.3/bits/stl_tree.h:1068:   instantiated from `std::_Rb_tree_iterator<_Val, _Val&, _Val*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(std::_Rb_tree_iterator<_Val, _Val&, _Val*>, const _Val&) [with _Key = std::string, _Val = std::pair<const std::string, std::auto_ptr<Toto> >, _KeyOfValue = std::_Select1st<std::pair<const std::string, std::auto_ptr<Toto> > >, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, std::auto_ptr<Toto> > >]'
    /usr/include/c++/3.3.3/bits/stl_map.h:364:   instantiated from `typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, _Alloc>::iterator, const std::pair<const _Key, _Tp>&) [with _Key = std::string, _Tp = std::auto_ptr<Toto>, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, std::auto_ptr<Toto> > >]'
    /usr/include/c++/3.3.3/bits/stl_map.h:319:   instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp = std::auto_ptr<Toto>, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, std::auto_ptr<Toto> > >]'
    t1.cc:21:   instantiated from here
    /usr/include/c++/3.3.3/bits/stl_pair.h:88: error: passing `const 
       std::auto_ptr<Toto>' as `this' argument of `std::auto_ptr<_Tp>::operator 
       std::auto_ptr_ref<_Tp1>() [with _Tp1 = Toto, _Tp = Toto]' discards 
       qualifiers
    une idée d'ou peut venir l'erreur ?

    Luther

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Août 2003
    Messages
    247
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 247
    Points : 276
    Points
    276
    Par défaut
    http://c.developpez.com/faq/cpp/?pag...NTEURS_autoptr


    On y lit, entre autre, que auto_ptr ne devrait pas être utilisé avec les conteneurs de la STL. Et je suis bien d'accord ;-).

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2005
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 15
    Points : 14
    Points
    14
    Par défaut
    Citation Envoyé par Selenite
    http://c.developpez.com/faq/cpp/?pag...NTEURS_autoptr


    On y lit, entre autre, que auto_ptr ne devrait pas être utilisé avec les conteneurs de la STL. Et je suis bien d'accord ;-).
    Je viens de lire la ref mais je ne comprend pas pk ?

    Luther

  4. #4
    Membre actif
    Profil pro
    Inscrit en
    Août 2003
    Messages
    247
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 247
    Points : 276
    Points
    276
    Par défaut
    Parce que concrètement, tes pointeurs seront invalides.

  5. #5
    Expert éminent sénior

    Homme Profil pro
    pdg
    Inscrit en
    Juin 2003
    Messages
    5 752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : pdg

    Informations forums :
    Inscription : Juin 2003
    Messages : 5 752
    Points : 10 683
    Points
    10 683
    Billets dans le blog
    3
    Par défaut
    Oui, mais il constatera ce problème à l'exécution. Là il a un problème de compilation, mais je sais pas trop pourquoi.

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2005
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 15
    Points : 14
    Points
    14
    Par défaut
    J'ai reçu a trouver une explication :

    Le debut de mon erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    /usr/include/c++/3.3.3/bits/stl_map.h:319:   instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp = std::auto_ptr<Toto>, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, std::auto_ptr<Toto> > >]'
    A la ligne 319 de stl_map.h:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
     mapped_type&
        operator[](const key_type& __k)
        {
          // concept requirements
          __glibcpp_function_requires(_DefaultConstructibleConcept<mapped_type>)
     
          iterator __i = lower_bound(__k);
          // __i->first is greater than or equivalent to __k.
          if (__i == end() || key_comp()(__k, (*__i).first))
            __i = insert(__i, value_type(__k, mapped_type())); // ligne 319
          return (*__i).second;
        }
    avec dans mon cas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    value_type(__k, mapped_type())
    <=>
    pair<string, Toto>
    Or dans le constructucteur de pair (stl_pair.h ligne 84) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) {}
    Donc il faut un constructeur par copie pour les deux elementde map.

    Le probleme est que les auto_ptr en n'ont pas !

    Luther

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    62
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Décembre 2004
    Messages : 62
    Points : 83
    Points
    83
    Par défaut
    Si, apparement il existe.
    C'est bizarre, j'ai essayé ton exemple avec BCB et ça compile bien.
    Tu utilises peut-être une STL non standard ?!?!

    Par contre je m'associe à Selenite pour la mise en garde.
    Il faut bien savoir comment fonctionne les auto_ptr pour s'amuser à ça!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    Public Member Functions
     	auto_ptr (element_type *__p=0) throw ()
     	An auto_ptr is usually constructed from a raw pointer.
     	auto_ptr (auto_ptr &a) throw ()
     	An auto_ptr can be constructed from another auto_ptr.
    template<typename Type1>  	auto_ptr (auto_ptr< Type1 > &a) throw ()
     	An auto_ptr can be constructed from another auto_ptr.
    auto_ptr & 	operator= (auto_ptr &a) throw ()
     	auto_ptr assignment operator.
    template<typename Type1> auto_ptr & 	operator= (auto_ptr< Type1 > &a) throw ()
     	auto_ptr assignment operator.

  8. #8
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2005
    Messages
    15
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 15
    Points : 14
    Points
    14
    Par défaut
    Pour l'aspect standard, j'utilise la STL de Linux donc je suis quasi sur qu'elle respect le standard...

    Par contre apres une recherche plus approfondi sur internet, j'ai trouvé un peu plus d'information :

    http://cpptips.hyperformix.com/cpptips/autoptr_contain

    et

    http://www.gotw.ca/publications/usin...ffectively.htm

    donc c'est logique qu'il n'est pas de constructeur par copie dans le template auto_ptr.

    Luther

  9. #9
    Expert éminent sénior

    Homme Profil pro
    pdg
    Inscrit en
    Juin 2003
    Messages
    5 752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : pdg

    Informations forums :
    Inscription : Juin 2003
    Messages : 5 752
    Points : 10 683
    Points
    10 683
    Billets dans le blog
    3
    Par défaut
    VC++ donne un seul message d'erreur apparement plus clair:
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(34): error C2558: class 'std::auto_ptr<_Ty>' : pas de constructeur de copie disponible ou le constructeur de copie est déclaré 'explicit'
    with
    [
    _Ty=Toto
    ]
    moi aussi je pensais que ça venait du constructeur explicit de auto_ptr, sauf que si je fait la même chose avec Toto, et utilise Toto dans la map, ça passe...

Discussions similaires

  1. probleme avec les maps
    Par teramp3 dans le forum SL & STL
    Réponses: 3
    Dernier message: 31/03/2008, 11h01
  2. [Collections] Problème de clés pour les Maps.
    Par berg dans le forum Collection et Stream
    Réponses: 10
    Dernier message: 01/03/2006, 21h20
  3. Problème de taille avec les tables
    Par Angelico dans le forum Paradox
    Réponses: 2
    Dernier message: 27/12/2004, 18h38
  4. [Layer] Probleme de superposition avec les JMenuItem
    Par azdruyel dans le forum Agents de placement/Fenêtres
    Réponses: 4
    Dernier message: 21/07/2004, 11h24
  5. problemes de performances avec les requetes select
    Par berry dans le forum Requêtes
    Réponses: 3
    Dernier message: 10/07/2003, 13h39

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