Newbie question: Serialization of an Array2D --> Cell** m_cells

Hello ! I am trying to serialize a a complex structure. One of the "variable" is a 2D Array (Cell** m_cells ) declared with pointers. I need to keep this data structure as it is. I have read some stuff about multi array with boost but found nothing to serialize my Cell** m_cells. Maybe one of you has the solution .... Note1: using boost 1.41.0, g++ and ubuntu 9.04 .... Note2: As you may understand while reading the code, I have in fact an Array2D of PointAccumulator .... (Cell = PointAccumulator) The part of code that is not correct ..... * template <class Archive> void serialize(Archive& ar, const unsigned int version){ *ar & m_xsize & m_ysize &* m_cells; }* And raising this error: wheelchair@wheelchair-laptop:~/playerstage/boostTest$ g++ -I/usr/local/include/ -L/usr/local/lib/ simple2.cpp -o simple -lboost_serialization /usr/local/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = PointAccumulator*]’: /usr/local/include/boost/serialization/serialization.hpp:74: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = PointAccumulator*]’ /usr/local/include/boost/serialization/serialization.hpp:133: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_iarchive, T = PointAccumulator*]’ /usr/local/include/boost/archive/detail/iserializer.hpp:173: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive, T = PointAccumulator*]’ simple2.cpp:186: instantiated from here /usr/local/include/boost/serialization/access.hpp:109: error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘PointAccumulator*’ /usr/local/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = PointAccumulator*]’: /usr/local/include/boost/serialization/serialization.hpp:74: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = PointAccumulator*]’ /usr/local/include/boost/serialization/serialization.hpp:133: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = PointAccumulator*]’ /usr/local/include/boost/archive/detail/oserializer.hpp:139: instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = PointAccumulator*]’ simple2.cpp:186: instantiated from here /usr/local/include/boost/serialization/access.hpp:109: error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘PointAccumulator*’ Here is the code I use for my tests: #include <iostream> #include <iterator> #include <algorithm> #include <sstream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; using namespace boost::archive; using namespace boost::serialization; /* * point * */ template <class T> struct point{ inline point():x(0),y(0) {} inline point(T _x, T _y):x(_x),y(_y){} T x, y; friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version){ ar & x & y; } }; /* * PointAccumulator * */ struct PointAccumulator{ typedef point<float> FloatPoint; PointAccumulator(): acc(0,0), n(0), visits(0){} PointAccumulator(int i): acc(0,0), n(0), visits(0){assert(i==-1);} static PointAccumulator* unknown_ptr; FloatPoint acc; int n, visits; friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version){ ar & n & visits & acc; } }; /* * Array2D * */ template<class Cell, const bool debug=false> class Array2D{ public: Array2D(int xsize=0, int ysize=0); Array2D& operator=(const Array2D &); Array2D(const Array2D<Cell,debug> &); //~Array2D(); inline Cell& cell(int x, int y); inline void print(){ cout << "Displaying array2D (x,y) --> (" << m_xsize << "," << m_ysize << ")" << endl; for(int i=0;i<m_xsize;i++){ for(int j=0;j<m_ysize;j++){ cout << cell(i,j).acc.x << " "; } cout<<endl; } } Cell ** m_cells; protected: int m_xsize, m_ysize; friend class boost::serialization::access; * template <class Archive> void serialize(Archive& ar, const unsigned int version){ ar & m_xsize & m_ysize & m_cells; }* }; template <class Cell, const bool debug> Array2D<Cell,debug> & Array2D<Cell,debug>::operator=(const Array2D<Cell,debug> & g){ if (debug || m_xsize!=g.m_xsize || m_ysize!=g.m_ysize){ for (int i=0; i<m_xsize; i++) delete [] m_cells[i]; delete [] m_cells; m_xsize=g.m_xsize; m_ysize=g.m_ysize; m_cells=new Cell*[m_xsize]; for (int i=0; i<m_xsize; i++) m_cells[i]=new Cell[m_ysize]; } for (int x=0; x<m_xsize; x++) for (int y=0; y<m_ysize; y++) m_cells[x][y]=g.m_cells[x][y]; if (debug){ std::cerr << __PRETTY_FUNCTION__ << std::endl; std::cerr << "m_xsize= " << m_xsize<< std::endl; std::cerr << "m_ysize= " << m_ysize<< std::endl; } return *this; } template <class Cell, const bool debug> Array2D<Cell,debug>::Array2D(int xsize, int ysize){ // assert(xsize>0); // assert(ysize>0); m_xsize=xsize; m_ysize=ysize; if (m_xsize>0 && m_ysize>0){ m_cells=new Cell*[m_xsize]; for (int i=0; i<m_xsize; i++) m_cells[i]=new Cell[m_ysize]; } else{ m_xsize=m_ysize=0; m_cells=0; } if (debug){ std::cerr << __PRETTY_FUNCTION__ << std::endl; std::cerr << "m_xsize= " << m_xsize<< std::endl; std::cerr << "m_ysize= " << m_ysize<< std::endl; } } template <class Cell, const bool debug> inline Cell& Array2D<Cell,debug>::cell(int x, int y){ return m_cells[x][y]; } template <class Cell, const bool debug> Array2D<Cell,debug>::Array2D(const Array2D<Cell,debug> & g){ m_xsize=g.m_xsize; m_ysize=g.m_ysize; m_cells=new Cell*[m_xsize]; for (int x=0; x<m_xsize; x++){ m_cells[x]=new Cell[m_ysize]; for (int y=0; y<m_ysize; y++) m_cells[x][y]=g.m_cells[x][y]; } if (debug){ std::cerr << __PRETTY_FUNCTION__ << std::endl; std::cerr << "m_xsize= " << m_xsize<< std::endl; std::cerr << "m_ysize= " << m_ysize<< std::endl; } } /* * SaveToFile and LoadFromFile * */ template <class A> void saveIntoFile(Array2D<PointAccumulator,false>& array, const char* file){ ofstream ofile(file); A ar(ofile); ar << array; ofile.close(); } template <class A> Array2D<PointAccumulator,false> loadFromFile(const char* file){ ifstream ifile(file); A ar(ifile); Array2D<PointAccumulator,false> array; ar >> array; ifile.close(); return array; } /* * MAIN * */ int main(){ Array2D<PointAccumulator,false> myArr = Array2D<PointAccumulator,false>(2,2); myArr.cell(0,0).acc.x = 1; myArr.cell(1,0).acc.x = 2; myArr.cell(0,1).acc.x = 1.1; myArr.cell(1,1).acc.x = 2.2; myArr.print(); saveIntoFile<text_oarchive>(myArr,"out.txt"); Array2D<PointAccumulator,false> myArr2 = loadFromFile<text_iarchive>("out.txt"); myArr2.print(); } Thanks for the help !

Simon Ruffieux wrote:
I have read some stuff about multi array with boost but found nothing to serialize my Cell** m_cells.
I have not looked in much detail at your question but based on the above alone perhaps this could help: https://svn.boost.org/svn/boost/sandbox/statistics/detail/multi_array/

Thanks for your answer I had a look at it, But it seems to me I would have to convert my Cell** into a boost::multi_array in order to use what you shown me ... Ideally I would keep my Cell** structure as it. And only be able to serialize it to save the array in a file and be able to reload it. Instead of converting it to a multi_array, I could also "flatten" it into a 1D array ... and serialize this 1D array but ideally I don't want to convert it ... If anyone has a simple example, it would be greatly appreciated ... Sorry I am new to Boost and I try to understand how to use it correctly ... cannot find simple tutorials that show what I need ... "er" <erwann.rogard@gmail.com> wrote in message news:hiis51$r7c$1@ger.gmane.org...
Simon Ruffieux wrote:
I have read some stuff about multi array with boost but found nothing to serialize my Cell** m_cells.
I have not looked in much detail at your question but based on the above alone perhaps this could help:
https://svn.boost.org/svn/boost/sandbox/statistics/detail/multi_array/ ----- Original Message ----- From: "er" <erwann.rogard@gmail.com> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> Sent: Tuesday, January 12, 2010 11:12 PM Subject: Re: Newbie question: Serialization of an Array2D -->Cell** m_cells
Simon Ruffieux wrote:
I have read some stuff about multi array with boost but found nothing to serialize my Cell** m_cells.
I have not looked in much detail at your question but based on the above alone perhaps this could help:
https://svn.boost.org/svn/boost/sandbox/statistics/detail/multi_array/
participants (2)
-
er
-
Simon Ruffieux