Boost logo

Boost Users :

From: Eric Melin (emelin_at_[hidden])
Date: 2007-10-30 09:43:47


Thanks Jeff, that fixed the issue.

________________________________

From: boost-users-bounces_at_[hidden] on behalf of Jeff Flinn
Sent: Tue 10/30/2007 7:18 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Serialization and raw pointer arrays

Hi Eric,

See inline below, which compiles for me.

Eric wrote:
> Hi,
> I've inherited some complex math code that makes large usage of Matrices
> based on raw pointer arrays. I need to serialize/deserialize these arrays
> as is and am looking to use boost::serialization package. My attempt at
> doing so results in the following errors (mostly about non integral array
> size). As I do not know the array size when it is created (and lots of code
> accesses the pointer directly as a chunk of memory that has been allocated,
> what are my options to get this to work?

I'm not sure what you're saying here, but to get your code to compile
see the inline addition/changes below

> Errors:
> /usr/include/boost/archive/detail/oserializer.hpp: In function `void
> boost::archive::save(Archive&, T&) [with Archive =
> boost::archive::text_oarchive, T = Dummy]':
> /usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from
> `void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int)
> [with T = Dummy, Archive = boost::archive::text_oarchive]'
> /usr/include/boost/archive/detail/interface_oarchive.hpp:78: instantiated
> from `Archive&
> boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T
> = Dummy, Archive = boost::archive::text_oarchive]'
> serialize_array.cpp:56: instantiated from here
> /usr/include/boost/archive/detail/oserializer.hpp:567: error: incomplete
> type `boost::STATIC_ASSERTION_FAILURE< false>' used in nested name specifier
> /usr/include/boost/archive/detail/oserializer.hpp:567: error: size of array
> has non-integral type `<type error>'
>
>
> Code:
> #include <boost/serialization/split_member.hpp>
> #include <fstream>
> #include <iostream>
>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
> using namespace std;
>
> class Dummy
> {

    BOOST_SERIALIZATION_SPLIT_MEMBER()

> public:
> friend class boost::serialization::access;
>
> Dummy() {pVal=NULL;size=0;}
> ~Dummy() {delete [] pVal;}
>
> template<class Archive>
> void save(Archive & ar, const unsigned int version) const
> {
> ar & size;
> for (int i=0; i<size; i++)
> {
> ar & pVal[i];
> }
> }
>
> template<class Archive>
> void load(Archive & ar, const unsigned int version)
> {
> ar & size;
> pVal = new int[size];
> for (int i=0; i<size; i++)
> {
> ar & pVal[i];
> }
> }
>
> int* pVal;
> int size;
> };
> BOOST_CLASS_VERSION(Dummy, 1)
>
> int main(int argc, char** argv)
> {
> Dummy d;
> d.size = 5;
> d.pVal = new int[d.size];
> for (int i=0; i< d.size; i++)
> {
> d.pVal[i] = i+10;
> }
>
> ofstream ofOut("testSer.txt");
> {
> boost::archive::text_oarchive oa(ofOut);
> oa << d;

This is the 'const' problem many run into. Robert should really add this
as a FAQ entry! I've found two options available:

        oa & d;

or

        oa << const_cast<const Dummy&>(d);

> }
>
> Dummy load;
> {
> ifstream ifIn("testSer.txt");
> boost::archive::text_iarchive ia(ifIn);
> ia >> load;
> }
> cout << "size="<<load.size<<endl;
>
> for (int i=0; i< load.size; i++)
> {
> cout << load.pVal[i] <<endl;
> }
>
> return -1;
> }
>
> Thanks,
> Eric

Jeff Flinn

_______________________________________________
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