Object Serialization Issue

P

Pallav singh

Hi All ,
i am getting core dump while Object Serialization of list <const
base * >
///////////////////////////////////////////////////////////////////////
#include <iostream>
/* Header file to Include FileStreams */
#include <fstream>
/* Header file to Include Archieving Files */
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
/* Derived classes should include serializations of their base
classes
*/
#include <boost/serialization/base_object.hpp>
/* List of containers to be serialized */
#include <boost/serialization/list.hpp>
/* To split Save() and Load() Function */
#include <boost/serialization/split_member.hpp>
////////////////////////////////////////////////////////////////////////
class gps_position
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
int degrees;
int minutes;
float seconds;
public:
gps_position(){};
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};

////////////////////////////////////////////////////////////////////////
class bus_stop
{
/* to proivide ACCESS TO private member variable of class */
friend class boost::serialization::access;
/* Always keep Serilization funtions in Private part of class
Else
* Object Tracking will be creating Problem for Polymorphic class
object
*/
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & latitude;
ar & longitude;
}
gps_position latitude;
gps_position longitude;
public:
bus_stop(const gps_position & lat_, const gps_position & long_) :
latitude(lat_), longitude(long_)
{}
bus_stop(){}
virtual ~bus_stop(){}
};

/////////////////////////////////////////////////////////////////////////// /
class bus_stop_corner : public bus_stop
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
ar & boost::serialization::base_object<bus_stop>(*this);
ar & street1;
ar & street2;
}
std::string street1;
std::string street2;
virtual std::string description() const
{
return street1 + " and " + street2;
}
public:
bus_stop_corner(){}
bus_stop_corner(const gps_position & lat_, const gps_position &
long_,
const std::string & s1_, const std::string & s2_
) :
bus_stop(lat_, long_), street1(s1_), street2(s2_)
{}
};

//////////////////////////////////////////////////////////////////////////
class Save_Polymorphic_Objects
{
friend class boost::serialization::access;
// Create List to serialize Polymorphic Objects
std::list<const bus_stop *> stops;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// note, version is always the latest when saving
std::cout <<" Template Function Save Called \n";
ar & stops;
}
public:
Save_Polymorphic_Objects(){}
void Insert_Polymorphic_Object(){
const gps_position position_10 (10, 10, 10.567f);
const gps_position position_20 (20, 20, 20.567f);
const gps_position position_11 (11, 11, 11.567f);
const gps_position position_21 (21, 21, 21.567f);
const gps_position position_12 (12, 12, 12.567f);
const gps_position position_22 (22, 22, 22.567f);
const gps_position position_13 (13, 13, 13.567f);
const gps_position position_23 (23, 23, 23.567f);
const gps_position position_14 (14, 14, 14.567f);
const gps_position position_24 (24, 24, 24.567f);
const bus_stop_corner bus_stop_corner_1( position_10,
position_20, "New Delhi_0", " Mumbai_0");
const bus_stop_corner bus_stop_corner_2( position_11,
position_21, "New Delhi_1", " Mumbai_1");
const bus_stop_corner bus_stop_corner_3( position_12,
position_22, "New Delhi_2", " Mumbai_2");
const bus_stop bus_stop_1( position_13, position_23);
const bus_stop bus_stop_2( position_14, position_24);
stops.push_back( &bus_stop_corner_1);
stops.push_back( &bus_stop_1);
stops.push_back( &bus_stop_2);
stops.push_back( &bus_stop_corner_2);
stops.push_back( &bus_stop_corner_3);
}
};

//////////////////////////////////////////////////////////////////////////
int main() {
std::eek:fstream ofs("msg_file_store");
Save_Polymorphic_Objects polymorphic_object_1;
polymorphic_object_1.Insert_Polymorphic_Object();
{
boost::archive::text_oarchive oa(ofs);
oa << polymorphic_object_1;
}
// Create a Dummy Object Location where we need to retieve Data
Save_Polymorphic_Objects polymorphic_object_2;
{
std::ifstream ifs("msg_file_store");
boost::archive::text_iarchive ia(ifs);
ia >> polymorphic_object_2;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
Thanks
Pallav Singh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top