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
dans la sérialisation de la classe Fille pour appeller la sérialisation de la classe Mere
Code : Sélectionner tout - Visualiser dans une fenêtre à part ar & boost::serialization::base_object<Mere>(*this);
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
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
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; }
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>'
Partager