Boost logo

Boost Users :

From: Eric (emelin_at_[hidden])
Date: 2007-10-29 17:49:01


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?

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
{
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;
    }

    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


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