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

Boost C++ Discussion :

Boost, héritage et sérialisation


Sujet :

Boost C++

  1. #1
    Membre averti
    Homme Profil pro
    Analyse système
    Inscrit en
    Novembre 2008
    Messages
    227
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Analyse système
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Novembre 2008
    Messages : 227
    Points : 311
    Points
    311
    Par défaut Boost, héritage et sérialisation
    Bonjour,
    J'utilise la librairie boost pour sérialiser en XML mes objets. J'arrive sans problème à sérialiser un objet tout bête, mais impossible de compiler la sérialisation d'un objet qui hérite d'une classe mère.

    Pour faire j'ai créer une classe Mere et une classe Fille qui hérite de Mere.
    Le problème se pose lorsque je rajoute la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ar & boost::serialization::base_object<Mere>(*this);
    dans la sérialisation de la classe Fille pour appeller la sérialisation de la classe Mere

    Voici mon code tout bête, tout est dans un seul fichier.

    Pour info j'ai parcouru le tutoriel Pierre Schwartz ainsi que la doc de Boost sur la sérialisation, mais je ne suis pas plus avancé pour l'instant

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
     
    #include <string>
    #include <iostream>
    #include <fstream>
     
    // Librairie Boost pour la serialisation
    // =====================================
    #include <boost/archive/xml_oarchive.hpp>     // Archive pour l'écriture XML
    #include <boost/archive/xml_iarchive.hpp>     // Archive pour la lecture des fichiers XML
    #include <boost/serialization/nvp.hpp>        // "Pair nom-valeur"
    #include <boost/serialization/string.hpp>
    #include <boost/serialization/version.hpp>
    #include <boost/serialization/base_object.hpp>
    // #include <boost/serialization/split_member.hpp>
     
    class Mere
    {
      public :
        Mere(int a=0) : m(a){};
     
      protected:
        int m;
     
      private:
        friend class boost::serialization::access;
        template<class Archive> void serialize( Archive &ar, const unsigned int version ) 
        {
          using boost::serialization::make_nvp;
          ar & make_nvp("attribut_mere",m);
        }
    };
     
    class Fille : public Mere
    {
      protected:
        double f;
     
      public :
        Fille(int a=0, double b=0.0) : Mere(a), f(b){};
     
      private:
        friend class boost::serialization::access;
        template<class Archive> void serialize( Archive &ar, const unsigned int version ) 
        {
          using boost::serialization::make_nvp;
          ar & boost::serialization::base_object<Mere>(*this);
          ar & make_nvp("attribut_fille",f);
        }
    };
     
    void sauve(const Mere &mere) 
    {
      std::ofstream ofile("SerialisationMere.xml");
      boost::archive::xml_oarchive oa(ofile);
      oa << BOOST_SERIALIZATION_NVP(mere);
    }
     
    void sauve(const Fille &fille) 
    {
      std::ofstream ofile("SerialisationFille.xml");
      boost::archive::xml_oarchive oa(ofile);
      oa << BOOST_SERIALIZATION_NVP(fille);
    }
     
     
    void testserialisation() 
    {
      std::cout << "==================================================================================" << std::endl;
      std::cout << "                            TESTS SERIALISATIONS" << std::endl;
      Mere m(1);
      sauve(m);
      Fille f(1,2.0);
      sauve(f);
      std::cout << "==================================================================================" << std::endl;
    }
    L'erreur de compilation est la suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    g++ -Wno-deprecated -MM -I. PatronMembre.cpp test.cpp testserialization.cpp > .Makefile.dep
    g++ -g -Wall -Wno-deprecated -I. -c testserialization.cpp -o ./testserialization.o
    /usr/include/boost/archive/basic_xml_oarchive.hpp: In member function 'void boost::archive::basic_xml_oarchive<Archive>::save_override(T&, int) [with T = const Mere, Archive = boost::archive::xml_oarchive]':
    /usr/include/boost/archive/detail/interface_oarchive.hpp:78:   instantiated from 'Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = const Mere, Archive = boost::archive::xml_oarchive]'
    /usr/include/boost/archive/detail/interface_oarchive.hpp:86:   instantiated from 'Archive& boost::archive::detail::interface_oarchive<Archive>::operator&(T&) [with T = Mere, Archive = boost::archive::xml_oarchive]'
    testserialization.cpp:45:   instantiated from 'void Fille::serialize(Archive&, unsigned int) [with Archive = boost::archive::xml_oarchive]'
    /usr/include/boost/serialization/access.hpp:109:   instantiated from 'static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_oarchive, T = Fille]'
    /usr/include/boost/serialization/serialization.hpp:81:   instantiated from 'void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_oarchive, T = Fille]'
    /usr/include/boost/serialization/serialization.hpp:140:   instantiated from 'void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_oarchive, T = Fille]'
    /usr/include/boost/archive/detail/oserializer.hpp:148:   instantiated from 'void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::xml_oarchive, T = Fille]'
    testserialization.cpp:73:   instantiated from here
    /usr/include/boost/archive/basic_xml_oarchive.hpp:86: erreur: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>'

  2. #2
    Membre expérimenté

    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
    Points : 1 543
    Points
    1 543
    Par défaut
    Salut,

    Si tu vas voir dans asic_xml_oarchive.hpp à ligne 86, tu vois juste au-dessus du BOOST_MPL_ASSERT :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
            // If your program fails to compile here, its most likely due to
            // not specifying an nvp wrapper around the variable to
            // be serialized.
    Une petite recherche dans la documentation de Boost.Serialization conduit à cette section.

    MAT.

  3. #3
    Membre émérite

    Inscrit en
    Mai 2008
    Messages
    1 014
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 1 014
    Points : 2 252
    Points
    2 252
    Par défaut
    Pourquoi dans ton code la serialization de la classe mère n'a pas les honneurs d'un make_nvp ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    template<class Archive> 
    void serialize( Archive &ar, const unsigned int version ) 
    {
       using boost::serialization::make_nvp;
    -  ar & boost::serialization::base_object<Mere>(*this);
    +  ar & make_nvp("mere", boost::serialization::base_object<Mere>(*this));
       ar & make_nvp("attribut_fille",f);
    }

  4. #4
    Membre averti
    Homme Profil pro
    Analyse système
    Inscrit en
    Novembre 2008
    Messages
    227
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Analyse système
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Novembre 2008
    Messages : 227
    Points : 311
    Points
    311
    Par défaut
    Merci à vous effectivement avec le make_nvp celà fonctionne.
    Je ne l'avais pas mis car dans tous les exemples que j'ai trouvé il n'y était pas.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [BOOST] Problème de sérialisation.
    Par Invité dans le forum Boost
    Réponses: 3
    Dernier message: 24/07/2014, 17h04
  2. Sérialisation en binaire (sans Boost)
    Par fleorock24 dans le forum C++
    Réponses: 7
    Dernier message: 10/06/2009, 08h51
  3. Réponses: 8
    Dernier message: 25/03/2008, 21h59
  4. Réponses: 6
    Dernier message: 31/08/2007, 21h05
  5. Sérialisation d'objets sans Boost
    Par Alp dans le forum Boost
    Réponses: 7
    Dernier message: 24/09/2006, 11h43

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