[Serialization] Restoring from text archive compile error

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

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
#include #include #include #include
try adding:
#include

Nope, I did miss that but that didn't fix it, I still get the same compilation errors. On 06/25/2010 02:36 PM, Jeff Flinn wrote:
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
#include #include #include #include try adding:
#include
I didn't look at any other errors, but this is the obvious ommision.
Jeff
Jeff
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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
#include #include #include #include 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
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

Yep that's what It was, thanks for the help, I guess I should have looked further into the error messages. Since however I didn't want to modify the implementation of the class, I used the const_cast on the constant members which worked just fine. I do have a couple of more general questions: 1) What is the benefit/reason for using BOOST_SERIALIZATION_ASSUME_ABSTRACT as it is used in the tutorial demo.cpp for an abstract class (it obviously works without it)? 2) In the case where the base class has no serializable members (like it is the case for the my "base" class), I get a warning for unused parameter ar as expected, however is there any defacto way to bypass that ? Best and thank you for your answers, Dante On 06/25/2010 03:52 PM, Jeff Flinn wrote:
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
#include #include #include #include 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
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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Dante Stroe wrote:
Yep that's what It was, thanks for the help, I guess I should have looked further into the error messages. Since however I didn't want to modify the implementation of the class, I used the const_cast on the constant members which worked just fine. I do have a couple of more general questions: 1) What is the benefit/reason for using BOOST_SERIALIZATION_ASSUME_ABSTRACT as it is used in the tutorial demo.cpp for an abstract class (it obviously works without it)?
the "is_abstract" type trait doesn't work on all platforms. BOOST_SERIALIZATION_ASSUME_ABSTRACT is a no-op on those platforms where "is_abstract does work and a specialization on those platforms where it does ... or something like that. It permits your code (and test code) to be run on all compilers.
2) In the case where the base class has no serializable members (like it is the case for the my "base" class), I get a warning for unused parameter ar as expected, however is there any defacto way to bypass that ?
You can get around that by explicitly defining a void_cast specialization. I think one of the test does that to avoid bogus warning. But it doesn't really add anything except shutting down the warning. Robert Ramey

The error listing contains the following near the top. /home/dante/pagmo/external_headers/boost/archive/detail/check.hpp:162: error: invalid application of sizeof to incomplete type boost::STATIC_ASSERTION_FAILURE<false> This is meant to be a clue as to where the problem is. check.hpp contains the following at line 162 template<class T> inline void check_const_loading(){ typedef BOOST_DEDUCED_TYPENAME mpl::or_< BOOST_DEDUCED_TYPENAME boost::serialization::is_wrapper<T>, BOOST_DEDUCED_TYPENAME mpl::not_< BOOST_DEDUCED_TYPENAME boost::is_const<T>
::type typex;
// cannot load data into a "const" object unless it's a // wrapper around some other non-const object. BOOST_STATIC_ASSERT(typex::value); } The comment at this point is meant to be another clue. What other information could be provided to make this easier. Robert Ramey
participants (3)
-
Dante Stroe
-
Jeff Flinn
-
Robert Ramey