Boost logo

Boost Users :

Subject: [Boost-users] Serialization of pointers ? --> loading not working ...
From: Simon Ruffieux (simon.ruffieux_at_[hidden])
Date: 2010-01-14 11:09:36


Hello again !

I am continuing my structure serialization ...
Seems I do not get something with pointers ... I have followed the
"demo.cpp" example and tried to do the same as they are doing for pointers
but I get an exception when trying to relaod my structure !

Hope someone can help me ....

What I do:
-Save a Particle structure.
    - this particle contains a SensoRreading class
        -this sensorReading has a pointer to a Sensor class (only containing
a string)

When I save(serialize) the particle, the file "out.txt" contains the
following text:
22 serialization::archive 6 0 0 0 0 1.1000000000000001 4.4000000000000004 0
2.1000000000000001 8.4000000000000004 0 3 12.699999999999999 8.5 6 0 0 35 3
1 0 0 11 Sensor Name

This seems correct to me !

But when I try to load it back in a particle using

    Particle p2 = Particle();
    loadFromFile<text_iarchive>(p2, "out.txt");

It raises the follwoing error during compilation:

wheelchair_at_wheelchair-laptop:~/playerstage/boostTest$<wheelchair_at_wheelchair-laptop:%7E/playerstage/boostTest$>g++
simple_RangeReading.cpp -o simple -lboost_serialization
/usr/local/include/boost/archive/detail/iserializer.hpp: In static member
function ‘static void
boost::archive::detail::load_pointer_type<Archive>::invoke(Archive&, Tptr&)
[with Tptr = const Sensor*, Archive = boost::archive::text_iarchive]’:
/usr/local/include/boost/archive/detail/iserializer.hpp:554: instantiated
from ‘void boost::archive::load(Archive&, T&) [with Archive =
boost::archive::text_iarchive, T = const Sensor*]’
/usr/local/include/boost/archive/detail/common_iarchive.hpp:61:
instantiated from ‘void
boost::archive::detail::common_iarchive<Archive>::load_override(T&, int)
[with T = const Sensor*, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/basic_text_iarchive.hpp:62: instantiated
from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&,
int) [with T = const Sensor*, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/text_iarchive.hpp:66: instantiated from
‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int)
[with T = const Sensor*, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/interface_iarchive.hpp:61:
instantiated from ‘Archive&
boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T
= const Sensor*, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/interface_iarchive.hpp:68:
instantiated from ‘Archive&
boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T =
const Sensor*, Archive = boost::archive::text_iarchive]’
simple_RangeReading.cpp:279: instantiated from ‘void
SensorReading::serialize(Archive&, unsigned int) [with Archive =
boost::archive::text_iarchive]’
/usr/local/include/boost/serialization/access.hpp:109: instantiated from
‘static void boost::serialization::access::serialize(Archive&, T&, unsigned
int) [with Archive = boost::archive::text_iarchive, T = SensorReading]’
/usr/local/include/boost/serialization/serialization.hpp:74: instantiated
from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with
Archive = boost::archive::text_iarchive, T = SensorReading]’
/usr/local/include/boost/serialization/serialization.hpp:133: instantiated
from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int)
[with Archive = boost::archive::text_iarchive, T = SensorReading]’
/usr/local/include/boost/archive/detail/iserializer.hpp:173: instantiated
from ‘void boost::archive::detail::iserializer<Archive,
T>::load_object_data(boost::archive::detail::basic_iarchive&, void*,
unsigned int) const [with Archive = boost::archive::text_iarchive, T =
SensorReading]’
simple_RangeReading.cpp:424: instantiated from here
/usr/local/include/boost/archive/detail/iserializer.hpp:486: error:
reinterpret_cast from type ‘const Sensor**’ to type ‘void**’ casts away
constness
/usr/local/include/boost/archive/detail/iserializer.hpp:489: error: no
matching function for call to
‘boost::archive::detail::load_pointer_type<boost::archive::text_iarchive>::pointer_tweak(const
boost::serialization::extended_type_info&, const Sensor*&, const Sensor&)’
/usr/local/include/boost/archive/detail/iserializer.hpp: In member function
‘void boost::archive::detail::pointer_iserializer<Archive,
T>::load_object_ptr(boost::archive::detail::basic_iarchive&, void*&,
unsigned int) const [with Archive = boost::archive::text_iarchive, T = const
Sensor]’:
simple_RangeReading.cpp:424: instantiated from here
/usr/local/include/boost/archive/detail/iserializer.hpp:286: error: invalid
conversion from ‘const void*’ to ‘void*’
/usr/local/include/boost/archive/detail/iserializer.hpp:294: error: invalid
conversion from ‘const void*’ to ‘void*’
/usr/local/include/boost/archive/detail/iserializer.hpp:294: error:
initializing argument 1 of ‘void
boost::archive::detail::basic_iarchive::next_object_pointer(void*)’
/usr/local/include/boost/serialization/access.hpp: In static member function
‘static void boost::serialization::access::serialize(Archive&, T&, unsigned
int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’:
/usr/local/include/boost/serialization/serialization.hpp:74: instantiated
from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with
Archive = boost::archive::text_iarchive, T = const Sensor]’
/usr/local/include/boost/serialization/serialization.hpp:133: instantiated
from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int)
[with Archive = boost::archive::text_iarchive, T = const Sensor]’
/usr/local/include/boost/archive/detail/iserializer.hpp:173: instantiated
from ‘void boost::archive::detail::iserializer<Archive,
T>::load_object_data(boost::archive::detail::basic_iarchive&, void*,
unsigned int) const [with Archive = boost::archive::text_iarchive, T = const
Sensor]’
simple_RangeReading.cpp:424: instantiated from here
/usr/local/include/boost/serialization/access.hpp:109: error: passing ‘const
Sensor’ as ‘this’ argument of ‘void Sensor::serialize(Archive&, unsigned
int) [with Archive = boost::archive::text_iarchive]’ discards qualifiers
/usr/local/include/boost/serialization/access.hpp: In static member function
‘static void boost::serialization::access::construct(T*) [with T = const
Sensor]’:
/usr/local/include/boost/serialization/serialization.hpp:98: instantiated
from ‘void boost::serialization::load_construct_data(Archive&, T*, unsigned
int) [with Archive = boost::archive::text_iarchive, T = const Sensor]’
/usr/local/include/boost/serialization/serialization.hpp:163: instantiated
from ‘void boost::serialization::load_construct_data_adl(Archive&, T*,
unsigned int) [with Archive = boost::archive::text_iarchive, T = const
Sensor]’
/usr/local/include/boost/archive/detail/iserializer.hpp:295: instantiated
from ‘void boost::archive::detail::pointer_iserializer<Archive,
T>::load_object_ptr(boost::archive::detail::basic_iarchive&, void*&,
unsigned int) const [with Archive = boost::archive::text_iarchive, T = const
Sensor]’
simple_RangeReading.cpp:424: instantiated from here
/usr/local/include/boost/serialization/access.hpp:123: error: invalid
conversion from ‘const void*’ to ‘void*’
/usr/local/include/boost/serialization/access.hpp:123: error: initializing
argument 2 of ‘void* operator new(size_t, void*)’
/usr/local/include/boost/archive/detail/check.hpp: In function ‘void
boost::archive::detail::check_const_loading() [with T = const Sensor]’:
/usr/local/include/boost/archive/detail/iserializer.hpp:539: instantiated
from ‘void boost::archive::load(Archive&, T&) [with Archive =
boost::archive::text_iarchive, T = const Sensor]’
/usr/local/include/boost/archive/detail/common_iarchive.hpp:61:
instantiated from ‘void
boost::archive::detail::common_iarchive<Archive>::load_override(T&, int)
[with T = const Sensor, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/basic_text_iarchive.hpp:62: instantiated
from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&,
int) [with T = const Sensor, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/text_iarchive.hpp:66: instantiated from
‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int)
[with T = const Sensor, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/interface_iarchive.hpp:61:
instantiated from ‘Archive&
boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T
= const Sensor, Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/serialization/nvp.hpp:87: instantiated from ‘void
boost::serialization::nvp<T>::load(Archivex&, unsigned int) [with Archivex =
boost::archive::text_iarchive, T = const Sensor]’
/usr/local/include/boost/serialization/access.hpp:101: instantiated from
‘static void boost::serialization::access::member_load(Archive&, T&,
unsigned int) [with Archive = boost::archive::text_iarchive, T =
boost::serialization::nvp<const Sensor>]’
/usr/local/include/boost/serialization/split_member.hpp:54: instantiated
from ‘static void boost::serialization::detail::member_loader<Archive,
T>::invoke(Archive&, T&, unsigned int) [with Archive =
boost::archive::text_iarchive, T = boost::serialization::nvp<const Sensor>]’
/usr/local/include/boost/serialization/split_member.hpp:69: instantiated
from ‘void boost::serialization::split_member(Archive&, T&, unsigned int)
[with Archive = boost::archive::text_iarchive, T =
boost::serialization::nvp<const Sensor>]’
/usr/local/include/boost/serialization/nvp.hpp:89: instantiated from ‘void
boost::serialization::nvp<T>::serialize(Archive&, unsigned int) [with
Archive = boost::archive::text_iarchive, T = const Sensor]’
/usr/local/include/boost/serialization/access.hpp:109: instantiated from
‘static void boost::serialization::access::serialize(Archive&, T&, unsigned
int) [with Archive = boost::archive::text_iarchive, T =
boost::serialization::nvp<const Sensor>]’
/usr/local/include/boost/serialization/serialization.hpp:74: instantiated
from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with
Archive = boost::archive::text_iarchive, T = boost::serialization::nvp<const
Sensor>]’
/usr/local/include/boost/serialization/serialization.hpp:133: instantiated
from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int)
[with Archive = boost::archive::text_iarchive, T =
boost::serialization::nvp<const Sensor>]’
/usr/local/include/boost/archive/detail/iserializer.hpp:349: instantiated
from ‘static void
boost::archive::detail::load_non_pointer_type<Archive>::load_only::invoke(Archive&,
const T&) [with T = boost::serialization::nvp<const Sensor>, Archive =
boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/iserializer.hpp:415: instantiated
from ‘static void
boost::archive::detail::load_non_pointer_type<Archive>::invoke(Archive&, T&)
[with T = const boost::serialization::nvp<const Sensor>, Archive =
boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/iserializer.hpp:554: instantiated
from ‘void boost::archive::load(Archive&, T&) [with Archive =
boost::archive::text_iarchive, T = const boost::serialization::nvp<const
Sensor>]’
/usr/local/include/boost/archive/detail/common_iarchive.hpp:61:
instantiated from ‘void
boost::archive::detail::common_iarchive<Archive>::load_override(T&, int)
[with T = const boost::serialization::nvp<const Sensor>, Archive =
boost::archive::text_iarchive]’
/usr/local/include/boost/archive/basic_text_iarchive.hpp:62: instantiated
from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&,
int) [with T = const boost::serialization::nvp<const Sensor>, Archive =
boost::archive::text_iarchive]’
/usr/local/include/boost/archive/text_iarchive.hpp:66: instantiated from
‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int)
[with T = const boost::serialization::nvp<const Sensor>, Archive =
boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/interface_iarchive.hpp:61:
instantiated from ‘Archive&
boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T
= const boost::serialization::nvp<const Sensor>, Archive =
boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/iserializer.hpp:307: instantiated
from ‘void boost::archive::detail::pointer_iserializer<Archive,
T>::load_object_ptr(boost::archive::detail::basic_iarchive&, void*&,
unsigned int) const [with Archive = boost::archive::text_iarchive, T = const
Sensor]’
simple_RangeReading.cpp:424: instantiated from here
/usr/local/include/boost/archive/detail/check.hpp:162: error: invalid
application of ‘sizeof’ to incomplete type
‘boost::STATIC_ASSERTION_FAILURE<false>’
/usr/local/include/boost/archive/detail/check.hpp:162: error: invalid
application of ‘sizeof’ to incomplete type
‘boost::STATIC_ASSERTION_FAILURE<false>’
wheelchair_at_wheelchair-laptop:~/playerstage/boostTest$<wheelchair_at_wheelchair-laptop:%7E/playerstage/boostTest$>

Here is the code:

#include <iostream>
#include <iterator>
#include <algorithm>
#include <sstream>
#include <fstream>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>

using namespace std;
using namespace boost::archive;
using namespace boost::serialization;

/*
 * point
 *
 */
template <class T> struct point{
    inline point():x(0),y(0) {}
    inline point(T _x, T _y):x(_x),y(_y){}
    T x, y;
    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive& ar, const unsigned int version){
        ar & x & y;
    }
};

/*
 * OrientedPoint
 *
 */
template <class T, class A>
struct orientedpoint: public point<T>{
  inline orientedpoint() : point<T>(0,0), theta(0) {};
    inline orientedpoint(const point<T>& p);
    inline void print(){cout << "(" << this->x << "," << this->y << "," <<
theta << ")";}
    A theta;

    friend class boost::serialization::access;
    template <class Archive> void serialize(Archive& ar, const unsigned int
version){
        ar & this->x & this->y & theta;
    }
};

template <class T, class A>
orientedpoint<T,A>::orientedpoint(const point<T>& p){
    this->x=p.x;
    this->y=p.y;
    this->theta=0.;
}

/*
 * SensorReading
 *
 */

class Sensor{
    public:
        Sensor(const std::string& name="");
        virtual ~Sensor();
        inline std::string getName() const {return m_name;}
        inline void setName(const std::string& name) {m_name=name;}

        friend class boost::serialization::access;
        template <class Archive> void serialize(Archive& ar, const unsigned
int version){
            ar & m_name;
        }
    protected:
        std::string m_name;
};

Sensor::Sensor(const std::string& name){
    m_name=name;
}

Sensor::~Sensor(){
}

class SensorReading{
    public:
        SensorReading(const Sensor* s=0, double time=0);
        virtual ~SensorReading();
        inline double getTime() const {return m_time;}
        inline void setTime(double t) {m_time=t;}
        inline const Sensor* getSensor() const {return m_sensor;}
        inline void print(){
            cout << "(" << m_time << "," << m_sensor->getName() << ")" <<
endl;
        }
        friend class boost::serialization::access;
        template <class Archive> void serialize(Archive& ar, const unsigned
int version){
            ar & m_time & m_sensor;
        }

    protected:
        double m_time;
        const Sensor* m_sensor;

};

SensorReading::SensorReading(const Sensor* s, double t){
    m_sensor=s;
    m_time=t;
}

SensorReading::~SensorReading(){}

typedef orientedpoint<double, double> OrientedPoint;
/*
 * Particle
 *
 */
struct Particle{
    /**constructs a particle, given a map
    @param map: the particle map
    */
    //Particle(const ScanMatcherMap& map);
    Particle(){};
    /** @returns the weight of a particle */
    inline operator double() const {return weight;}
    /** @returns the pose of a particle */
    inline operator OrientedPoint() const {return pose;}
    /** sets the weight of a particle
    @param w the weight
    */
    inline void setWeight(double w) {weight=w;}
    /** Display a particle */
    inline void print(){
        cout << endl<< "Particle: " << endl;
        cout << "-----------------" << endl;
        cout << "OrientedPoint pose: "; pose.print(); cout << endl;
        cout << "OrientedPoint previousPose: "; previousPose.print(); cout
<< endl;
        cout << "Weight: "<< weight << endl;
        cout << "WeightSum: "<< weightSum << endl;
        cout << "gweight: "<< gweight << endl;
        cout << "previousIndex: "<< previousIndex << endl;
        cout << "SensorReading: "; sr.print(); cout << endl;

    }
    /** The map */
    //ScanMatcherMap map;
    /** The pose of the robot */
    OrientedPoint pose;

    /** The pose of the robot at the previous time frame (used for computing
thr odometry displacements) */
    OrientedPoint previousPose;

    /** The weight of the particle */
    double weight;

    /** The cumulative weight of the particle */
    double weightSum;

    double gweight;

    /** The index of the previous particle in the trajectory tree */
    int previousIndex;

    /** Entry to the trajectory tree */
    //TNode* node;

    //Tests
    SensorReading sr;

    friend class boost::serialization::access;
    template <class Archive> void serialize(Archive& ar, const unsigned int
version){
        ar & pose & previousPose & weight & gweight & weightSum &
previousIndex & sr;
    }

};

/*
 * Particle
 * SaveToFile and LoadFromFile
 *
 */
template <class A> void saveIntoFile(Particle& particle, const char* file){
        ofstream ofile(file);
        A ar(ofile);
        ar << particle;
        ofile.close();
}

template <class A> void loadFromFile(Particle& particle, const char* file){
    ifstream ifile(file);
    A ar(ifile);
    ar >> particle;
    ifile.close();
    //return array;
}

/*
 * MAIN
 *
 */
int main(){

    Particle p = Particle();

    p.pose = OrientedPoint(point<double>(1.1,4.4));
    p.previousPose = OrientedPoint(point<double>(2.1,8.4));
    p.weight = 3;
    p.weightSum = 8.5;
    p.gweight = 12.7;
    p.previousIndex = 6;

    Sensor s= Sensor("Sensor Name");
    p.sr = SensorReading(&s,35);

    p.print();
    saveIntoFile<text_oarchive>(p, "out.txt");

    Particle p2 = Particle();

    loadFromFile<text_iarchive>(p2, "out.txt");

    p2.print();

    cout << "end" << endl;

    return 0;
}



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