Boost logo

Boost Users :

From: sjdf (sjdf.maillist_at_[hidden])
Date: 2008-01-01 22:15:38


The program run ok when I compile it with mingw, but if I compile it
use vc71, the program will crash when excute "pbb->Load( ifs );".

Thinks!

code:

//----------------------------------------------------------------------------
#include <string>
#include <iostream>

#include <fstream>

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

//----------------------------------------------------------------------------
using namespace std;

//----------------------------------------------------------------------------
class A
{
    private:
        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive & ar, const unsigned int version)
            {
                ar & a;
                ar & b;
            }

    public:
        int a;
        int b;

        void Load( std::ifstream& ifs )
        {
            boost::archive::text_iarchive ia(ifs);
            ia >> *this;
        }

        void Save( std::ofstream& ofs )
        {
            boost::archive::text_oarchive oa(ofs);
            const A& refa = *this;
            oa << refa;
        }
};

//----------------------------------------------------------------------------
class B
{
    private:
        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive & ar, const unsigned int version)
            {
                ar & ms;
                ar & a;
            }
    public:
        void Load( std::ifstream& ifs )
        {
            boost::archive::text_iarchive ia(ifs);
            ia >> *this;
        }

        void Save( std::ofstream& ofs )
        {
            boost::archive::text_oarchive oa(ofs);
            const B& refb = *this;
            oa << refb;
        }

    public:
        B( const string& s ) : ms( s ){}
        void print(){ cout << ms << endl; }
    private:
        string ms;
        A a;
};

//----------------------------------------------------------------------------
int test_TestStorage()
{
    // Save
    A* pa = new A;
    pa->a = 3;
    pa->b = 5;

    B* pb = new B( "this is test B" );

    std::ofstream ofs( "all.out" );

    pa->Save( ofs );
    pb->Save( ofs );

    delete pa;
    delete pb;
    ofs.close();

    // Load
    A* paa = new A;
    B* pbb = new B( "" );

    std::ifstream ifs( "all.out", std::ios::binary );

    paa->Load( ifs );
    pbb->Load( ifs );

    cout << "A: " << paa->a << " " << paa->b << endl;
    pbb->print();

    delete paa;
    delete pbb;

    return 0;
}

//----------------------------------------------------------------------------
int main( void )
{
    test_TestStorage();

    return 0;
}

2007/12/31, Mahesh Venkitachalam <mkvenkit.vc_at_[hidden]>:
> In my application, I am am saving 2 objects into the same archive and then
> reading
> them back correctly in the same order - so it is possible. Maybe you could
> post how
> you implemented "Load" and "Save" and I can try to help you.
>
> Mahesh
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


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