Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization] Restoring from text archive compile error
From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2010-06-25 09:52:14


Dante Stroe wrote:
> Hello,
>
> I am currently trying to serialize the classes of a project I am working
> on, but I ran into a few compliation errors and I am kind of stuck, so I
> was hoping somebody here could help me on that.
> I am using the 1.43 version of the boost library and gcc 4.4.1. (I'd
> like to add that all the examples in and tutorials from the
> documentation work fine for me).
> I am trying to serialize a base class called "base" and it's derived
> class called "de" that use a shared pointer, and afterwards test my
> implementation by saving the an instance of a "de" object into a text
> archive and then restoring it.
>
> These is the serialization implementation for my two classes for reference:
>
> ---"base.h"-----
> #include<iostream>
> #include<string>
> #include<typeinfo>
> #include "../config.h"
> #include "../population.h"
> #include "../rng.h"
> #include<boost/shared_ptr.hpp>
> #include<boost/archive/text_iarchive.hpp>
> #include<boost/archive/text_oarchive.hpp>
> #include<boost/serialization/version.hpp>
> #include<boost/serialization/base_object.hpp>
>
> namespace pagmo { namespace algorithm {
>
> /// Base algorithm class.
> class base;
>
> /// Alias for shared pointer to base algorithm.
> typedef boost::shared_ptr<base> base_ptr;
>
> /// Base algorithm class.
>
> class __PAGMO_VISIBLE base
>
> {
> public:
>
> base();
> /// Evolve method.
> virtual void evolve(population&p) const = 0;
> /// Clone method.
> virtual base_ptr clone() const = 0;
> virtual ~base();
> std::string human_readable() const;
> virtual std::string get_name() const;
> virtual bool is_blocking() const;
>
> protected:
>
> virtual std::string human_readable_extra() const;
>
> protected:
>
> /// Random number generator for double-precision floating point
> values.
> mutable rng_double m_drng;
> /// Random number generator for unsigned integer values.
> mutable rng_uint32 m_urng;
>
> private:
>
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive&ar, const unsigned int version){
> std::cout<< "Serializing base algorithm"<< version<<
> std::endl;
> }
>
> };
> }} //namespaces
>
> std::ostream __PAGMO_VISIBLE_FUNC&operator<<(std::ostream&, const base&);
> --------------------
>
> --------"de.h"------
>
> #include<string>
> #include "../config.h"
> #include "../population.h"
> #include "base.h"
> #include<boost/serialization/base_object.hpp>
>
> namespace pagmo { namespace algorithm {
>
> class __PAGMO_VISIBLE de: public base {
>
> public:
>
> de(int, const double& = 0.8, const double& = 0.9, int = 2);
> base_ptr clone() const;
> void evolve(population&) const;
> std::string get_name() const;
>
> protected:
>
> std::string human_readable_extra() const;
>
> private:
>
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive&ar, const unsigned int version){
> std::cout<< "Serializing de algorithm"<< version<< std::endl;;
> ar& boost::serialization::base_object<base>(*this);
> ar& m_gen;
> ar& m_f;
> ar& m_cr;
> ar& m_strategy;
> }
>
> const int m_gen;
> const double m_f;
> const double m_cr;
> const int m_strategy;
> };
>
> }} //namespaces
> ------------------------------
>
> Saving works fine (and as far as I can tell, based on the documentation
> examples the information is saved correctly in the text archive). This
> is the program that I am using to save and restore from archive:

Looks like you are trying to modify const members when de-serializing.
Can you remove the const's? Again, this is just a quick perusal of your
posting. You should provide a paired down example demonstrating you
problem, which will probably lead you to your problems.

Jeff

Jeff


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net