Sign in to follow this  
NeilFang

newbie question about boost::serialization

Recommended Posts

The code blow copy from the Boost::serialization tutorial,A Very Simple Case section
int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");

    // create class instance
    const gps_position g(35, 59, 24.567f);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
    	// archive and stream closed when destructors are called
}


If I remove the const away from instance define, as this "gps_position g(35, 59, 24.567f);", then I get a compile error. Could anybody tell why? Only const object can be serialized?

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
I don't know. What is the compile error you get?


I'm using Visual Studio 2005, the compile error is :

1>d:\boost\boost\archive\detail\oserializer.hpp(566) : error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'
1> with
1> [
1> x=false
1> ]
1> d:\boost\boost\archive\detail\common_oarchive.hpp(62) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=gps_position
1> ]
1> d:\boost\boost\archive\basic_text_oarchive.hpp(75) : see reference to function template instantiation 'void boost::archive::detail::common_oarchive<Archive>::save_override<T>(T &,int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=gps_position
1> ]
1> d:\boost\boost\archive\detail\interface_oarchive.hpp(79) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<T>(T &,int)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=gps_position
1> ]
1> d:\lpython\tryboost\simpleboost\example.cpp(47) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<gps_position>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::text_oarchive,
1> T=gps_position
1> ]

Share this post


Link to post
Share on other sites
That is intentional. See this doc about why boost::serialization fails to compile with non-const values. To be honest, that doesn't make a lot of sense to me and I haven't taken the time to read it carefully. I haven't managed to get boost::serialization to work for anything but trivial examples. The order that headers are included is very touchy and there are some other issues, but those are supposed to be improved in Boost 1.36 or something like that. If you can get it to work before then, that's great, but I might recommend you look for other solutions until then...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this