Does the demo included with the library work?
 
Robert Ramey
 
"wei niu" <wei.niu@gmail.com> wrote in message news:58418a380708240143g4993867er7fa68758127f4c5a@mail.gmail.com...
I cannot serialize an object through a pointer.When running the program,it reports:

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Below is the code :

//////////////////////////////////////////////////////////////////////////////////////////////
//
//    static link library: libboost_serialization-vc71-s-1_33_1.lib
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <list>

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

#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>

using std::cout ;
using std::endl ;

class gps_position
{
    public:
        int m_degrees ;
        int m_minu
        float m_seconds ;
        gps_position()
        {
            m_degrees = m_minutes = m_seconds = 0 ;
        }
        gps_position( int degrees, int minutes, float seconds )
        {
            m_degrees = degrees ;
            m_minutes = minutes ;
            m_seconds = seconds ;
        }
};

std::ostream& operator << ( std::ostream &out, const gps_position &g )
{
    return out << g.m_degrees << '\'' << g.m_minutes << '\'' << g.m_seconds ;
}

namespace boost{
    namespace serialization{

        template <class Archive>
        void serialize( Archive &ar, gps_position &g, const unsigned int version )
        {
            ar & g.m_degrees ;
            ar & g.m_minutes ;
            ar & g.m_seconds ;
        }

    }
}

class bus_stop
{
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & latitude;
        ar & longitude;
    }
    gps_position latitude;
    gps_position longitude;
protected:
    bus_stop(const gps_position & lat_, const gps_position & long_) :
    latitude(lat_), longitude(long_)
    {}
public:
    bus_stop(){}
    // See item # 14 in Effective C++ by Scott Meyers.
    // re non-virtual destructors in base classes.
    virtual ~bus_stop(){}
    friend std::ostream& operator << ( std::ostream &out, const bus_stop &bs ) ;
};

std::ostream& operator << ( std::ostream &out, const bus_stop &bs )
{
    return out << bs.latitude << '\t' << bs.longitude ;
}

class bus_stop_corner : public bus_stop
{
    friend class boost::serialization::access;
    std::string street1;
    std::string street2;
    virtual std::string description() const
    {
        return street1 + " and " + street2;
    }
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        // save/load base class information
        ar & boost::serialization::base_object<bus_stop>(*this);
        ar & street1 & 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)
    {
    }
    friend std::ostream& operator << ( std::ostream &out, const bus_stop_corner &bsc ) ;
};

std::ostream& operator << ( std::ostream &out, const bus_stop_corner &bsc )
{
    return out << dynamic_cast<const bus_stop&>(bsc) << ' ' << bsc.description() ;
}

class bus_route
{
    friend class boost::serialization::access;
    bus_stop * stops[2];
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        int i;
        for(i = 0; i < 2; ++i)
            ar & stops[i];
    }
public:
    bus_route()
    {
        stops[0] = stops[1] = 0 ;
    }
    bus_route( bus_stop *bs1, bus_stop *bs2 )
    {
        stops[0] = bs1, stops[1] = bs2 ;
    }
};


int main()
{
    const char *file_name = "non_intrusive.dat" ;

    bus_stop *bs0 = new bus_stop_corner(
    gps_position(34, 135, 52.560f),
    gps_position(134, 22, 78.30f),
    "24th Street", "10th Avenue"
    );

    bus_stop *bs1 = new bus_stop_corner(
    gps_position(35, 137, 23.456f),
    gps_position(133, 35, 54.12f),
    "State street", "Cathedral Vista Lane"
    );
    
    const bus_route br( bs0, bs1 ) ;
    {
        std::ofstream ofs( file_name ) ;
        boost::archive::text_oarchive oa( ofs ) ;
        oa << (br) ;
    }

    bus_route newbr ;
    {
        std::ifstream ifs( file_name ) ;
        boost::archive::text_iarchive ia( ifs ) ;
        ia >> newbr ;
    }
    
    return 0 ;
}

I think maybe it's because I didn't build the serialize library correctly.I builded it with bjam.When debuging it with VS2003,an exception was thrown in the _heap_alloc_base (size_t) function at line 211:
line 211: return HeapAlloc(_crtheap, 0, size);

Function "void * __cdecl _heap_alloc_base (size_t size)" is in malloc.c.


_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users