boost::serialization [resolu]

boost::serialization [resolu] - C++ - Programmation

Marsh Posté le 09-06-2006 à 14:03:58    

Salut les jeunes,
 
Bon j'essaie d'utiliser boost::serialization pour me faciliter la tache de sauvegarde, mais comme je suis une quiche, je n'arrive a rien (en gros ca ne compile pas pour commencer).
Quelqu'un peut m'aider ?
 
Foo.hpp

Code :
  1. // Foo.hpp -*- C++ -*-
  2. #ifndef _FOO_HPP_
  3. #define _FOO_HPP_
  4. #include <boost/serialization/serialization.hpp>
  5. class Foo {
  6.  friend class boost::serialization::access;
  7.  template<class Archive>
  8.  void serialize(Archive & ar, const unsigned int version) {
  9.   ar & value;
  10.   ar & valueBest;
  11.  }
  12. public:
  13.  Foo();
  14.  virtual ~Foo();
  15. //private:
  16.   double value;
  17.  double valueBest;
  18. };
  19. #endif


 
Foo.cpp

Code :
  1. // Foo.cpp -*- C++ -*-
  2. #include "Foo.hpp"
  3. #include <fstream>
  4. // include headers that implement an archive in simple text format
  5. #include <boost/archive/text_oarchive.hpp>
  6. Foo::Foo(): value(0), valueBest(0)  {
  7. }
  8. Foo::~Foo() {
  9. }
  10. void saveFoo(const Foo &s, const char * filename){
  11.     // make an archive
  12.     std::ofstream ofs(filename);
  13.     boost::archive::text_oarchive oa(ofs);
  14.     oa << s;
  15. }


 
Et l'erreur de compilation:

Code :
  1. g++ -Wall -c Foo.cpp
  2. /usr/include/boost/archive/detail/oserializer.hpp: In constructor 'boost::archive::detail::oserializer<Archive, T>::oserializer() [with Archive = boost::archive::text_oarchive, T = Foo]':
  3. /usr/include/boost/archive/detail/oserializer.hpp:135:   instantiated from 'static boost::archive::detail::oserializer<Archive, T>& boost::archive::detail::oserializer<Archive, T>::instantiate() [with Archive = boost::archive::text_oarchive, T = Foo]'
  4. /usr/include/boost/archive/detail/oserializer.hpp:264:   instantiated from 'static void boost::archive::detail::save_non_pointer_type<Archive, T>::save_standard::invoke(Archive&, const T& ) [with Archive = boost::archive::text_oarchive, T = Foo]'
  5. /usr/include/boost/archive/detail/oserializer.hpp:322:   instantiated from 'static void boost::archive::detail::save_non_pointer_type<Archive, T>::invoke(Archive&, const T& ) [with Archive = boost::archive::text_oarchive, T = Foo]'
  6. /usr/include/boost/archive/detail/oserializer.hpp:536:   instantiated from 'void boost::archive::save(Archive&, const T& ) [with Archive = boost::archive::text_oarchive, T = Foo]'
  7. /usr/include/boost/archive/basic_text_oarchive.hpp:78:   instantiated from 'void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = const Foo, Archive = boost::archive::text_oarchive]'
  8. /usr/include/boost/archive/detail/interface_oarchive.hpp:78:   instantiated from 'Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T& ) [with T = const Foo, Archive = boost::archive::text_oarchive]'
  9. Foo.cpp:18:   instantiated from here
  10. /usr/include/boost/archive/detail/oserializer.hpp:107: error: incomplete type 'boost::serialization::extended_type_info_null<Foo>' used in nested name specifier
  11. /usr/include/boost/archive/detail/oserializer.hpp: In member function 'bool boost::archive::detail::oserializer<Archive, T>::is_polymorphic() const [with Archive = boost::archive::text_oarchive, T = Foo]':
  12. Foo.cpp:18:   instantiated from here
  13. /usr/include/boost/archive/detail/oserializer.hpp:131: error: invalid use of undefined type 'struct boost::serialization::extended_type_info_null<Foo>'
  14. /usr/include/boost/serialization/type_info_implementation.hpp:26: error: declaration of 'struct boost::serialization::extended_type_info_null<Foo>'
  15. /usr/include/boost/archive/detail/oserializer.hpp:132: error: invalid use of undefined type 'struct boost::serialization::extended_type_info_null<Foo>'
  16. /usr/include/boost/serialization/type_info_implementation.hpp:26: error: declaration of 'struct boost::serialization::extended_type_info_null<Foo>'
  17. make: *** [Foo.o] Error 1


 
Merci de votre aide :)


Message édité par marmotte.tranquille le 09-06-2006 à 15:48:20
Reply

Marsh Posté le 09-06-2006 à 14:03:58   

Reply

Marsh Posté le 09-06-2006 à 15:25:57    

Salut,  
moi à la compilation, j'ai pas les meme erreurs (sous windows) (aux numéros de ligne près) bon d'accord ca fait pas avancer l'histoire, mais bon

Code :
  1. C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp: In constructor `boost::archive::detail::oserializer<Archive, T>::oserializer() [with Archive = boost::archive::text_oarchive, T = Foo]':
  2. C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp:135:   instantiated from `static boost::archive::detail::oserializer<Archive, T>& boost::archive::detail::oserializer<Archive, T>::instantiate() [with Archive = boost::archive::text_oarchive, T = Foo]'
  3. C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp:264:   instantiated from `static void boost::archive::detail::save_non_pointer_type<Archive, T>::save_standard::invoke(Archive&, const T& ) [with Archive = boost::archive::text_oarchive, T = Foo]'
  4. C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp:322:   instantiated from `static void boost::archive::detail::save_non_pointer_type<Archive, T>::invoke(Archive&, const T& ) [with Archive = boost::archive::text_oarchive, T = Foo]'
  5. C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp:536:   instantiated from `void boost::archive::save(Archive&, const T& ) [with Archive = boost::archive::text_oarchive, T = Foo]'
  6. C:/Boost/include/boost-1_33_1/boost/archive/basic_text_oarchive.hpp:78:   instantiated from `void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = const Foo, Archive = boost::archive::text_oarchive]'
  7. C:/Boost/include/boost-1_33_1/boost/archive/detail/interface_oarchive.hpp:78:   instantiated from `Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T& ) [with T = const Foo, Archive = boost::archive::text_oarchive]'
  8. Foo.cpp:63:   instantiated from here
  9. C:/Boost/include/boost-1_33_1/boost/archive/detail/oserializer.hpp:108: error: incomplete type `boost::serialization::extended_type_info_null<Foo>' used in nested name specifier


 
En fait, ton erreur, se situe dans cette méthode :
 

Code :
  1. void saveFoo(const Foo &s, const char * filename){
  2.    // make an archive
  3.    std::ofstream ofs(filename);
  4.    boost::archive::text_oarchive oa(ofs);
  5.   oa << s;
  6. }


 
sans le

Code :
  1. oa << s;

le code compile, cherche a ce niveau la


Message édité par turn3r le 09-06-2006 à 15:34:08

---------------
Les ventes de véhicules neufs ont chuté de 7%, y'en a marre des pirates qui téléchargent illégalement des voitures sur internet !!
Reply

Marsh Posté le 09-06-2006 à 15:30:33    

C'est bizarre parce que je n'ai fait qu'un copier-coller. Peut-etre as-tu une version de boost differente de la mienne (1.33.1)
 
Sinon j'ai bien remarquee que l'erreur vient de la (il fait appel a la methode template serialize) ;)
 
 
Sinon j'ai trouve une solution qui marche mais je ne sais pas vraiment pourquoi:
 
On vire le .hpp et on definit la classe (ainsi que les fonctions/methodes) dans le .cpp, comme ca:

Code :
  1. // Foo.cpp -*- C++ -*-
  2. #include <fstream>
  3. // include headers that implement an archive in simple text format
  4. #include <boost/archive/text_oarchive.hpp>
  5. #include <boost/serialization/serialization.hpp>
  6. class Foo {
  7.   double value;
  8.  double valueBest;
  9.  friend class boost::serialization::access;
  10.  template<class Archive>
  11.  void serialize(Archive & ar, const unsigned int version) {
  12.   ar & value;
  13.   ar & valueBest;
  14.  }
  15. public:
  16.  Foo(): value(0), valueBest(0)  {
  17.  }
  18.  virtual ~Foo() {}
  19. };
  20. void saveFoo(const Foo &s, const char * filename){
  21.     // make an archive
  22.     std::ofstream ofs(filename);
  23.     boost::archive::text_oarchive oa(ofs);
  24.     oa << s;
  25. }


 
Mais du coup si apres je cree une autre classe dans un autre fichier, qui a besoin de connaitre la structure Foo, comme puis-je inclure le header ?


Message édité par marmotte.tranquille le 09-06-2006 à 15:33:57
Reply

Marsh Posté le 09-06-2006 à 15:37:36    

Code :
  1. //Foo.hpp
  2. #ifndef _FOO_HPP_
  3. #define _FOO_HPP_
  4. #include <fstream>
  5. // include headers that implement an archive in simple text format
  6. #include <boost/archive/text_oarchive.hpp>
  7. #include <boost/serialization/serialization.hpp>
  8. class Foo {
  9.      double value;
  10.      double valueBest;
  11.      friend class boost::serialization::access;
  12.      template<class Archive>
  13.      void serialize(Archive & ar, const unsigned int version) {
  14.             ar & value;
  15.             ar & valueBest;
  16.      }
  17. public:
  18.      Foo(): value(0), valueBest(0)  {
  19.      }
  20.      virtual ~Foo() {}
  21. };
  22. #endif


 

Code :
  1. //Foo.cpp
  2. #include "Foo.hpp"
  3. void saveFoo(const Foo &s, const char * filename){
  4.    // make an archive
  5.    std::ofstream ofs(filename);
  6.    boost::archive::text_oarchive oa(ofs);
  7.    oa << s;
  8. }


 
En fait, tes fonction templates, doivent etre inline, c'est à dire, déclarées et implémentées dans le .h. Fallait juste rajouter les {}  :pfff:


Message édité par turn3r le 09-06-2006 à 15:40:29

---------------
Les ventes de véhicules neufs ont chuté de 7%, y'en a marre des pirates qui téléchargent illégalement des voitures sur internet !!
Reply

Marsh Posté le 09-06-2006 à 15:42:39    

Ouah merci ! Je pouvais chercher encore longtemps!


Message édité par marmotte.tranquille le 09-06-2006 à 15:42:55
Reply

Marsh Posté le 09-06-2006 à 16:18:44    

Tiens je viens de remarquer autre chose en fait.
Le code suivant fonctionne aussi et me convient mieux car on fait la difference entre la declaration et l'implementation :
 

Code :
  1. // Foo.hpp -*- C++ -*-
  2. #ifndef _FOO_HPP_
  3. #define _FOO_HPP_
  4. #include <boost/serialization/serialization.hpp>
  5. class Foo {
  6. private:
  7.  double value;
  8.  double valueBest;
  9.  friend class boost::serialization::access;
  10.  template<class Archive>
  11.  void serialize(Archive & ar, const unsigned int version) {
  12.   ar & value;
  13.   ar & valueBest;
  14.  }
  15. public:
  16.  Foo();
  17.  virtual ~Foo();
  18. };
  19. #endif


 
 

Code :
  1. // Foo.cpp -*- C++ -*-
  2. #include <fstream>
  3. #include <boost/archive/text_oarchive.hpp>
  4. #include "Foo.hpp"
  5. Foo::Foo(): value(0), valueBest(0)  {
  6. }
  7. Foo::~Foo() {
  8. }
  9. void saveFoo(const Foo &s, const char * filename){
  10.  std::ofstream ofs(filename);
  11.  boost::archive::text_oarchive oa(ofs);
  12.  oa << s;
  13. }


 
La seule difference avec ce que j'avais fait au debut est d'inclure fstream et text_oarchive avant Foo.hpp


Message édité par marmotte.tranquille le 09-06-2006 à 16:19:34
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed