I checked your code and it also did not work when I tried to compile it. However, based on the documentation for Boost Serialize, I get the impression that it is intended for use with the stream operator <. The following code works fine for me:
namespace boost { namespace serialization { template <class Archive, typename Derived> void serialize( Archive & ar, Eigen::EigenBase<Derived> & g, const unsigned int version){ ar & boost::serialization::make_array(g.derived().data(), g.size()); } } } int main (int argc, char* argv[]){ std::ofstream out("my_archive"); boost::archive::text_oarchive oa (out); Eigen::Matrix <double, 4, 4> a; out << a; return 0; }
The my_archive file is created in the working folder with non-zero values (just uninitialized garbage in memory, with the simplified code above).
EDIT: I tried using the exact code above in my own application and found that I got the same error as you. I honestly don't understand why this is, right now. The simplest fix I found was to replace Eigen::EigenBase<Derived> with the actual matrix type. Current code I'm using:
namespace boost{ namespace serialization { template <class Archive, typename Scalar> void serialize ( Archive & ar, Eigen::Matrix<Scalar, -1, -1, 0, -1, -1> & g, const unsigned int version ){ } } }
The above code works for any scalar type (float, double, int) and for dynamic / medium-term matrices. For static size, check and update the template settings accordingly.
EDIT No. 2 (April 09, 2014):
Despite the appearance of the above code, when I tried to fully integrate it into my code and implement it using the appropriate unit testing, it stops working. Unfortunately, the error messages I was given - both from Visual Studio and from clang - were useless. Fortunately, gcc was buried in a terrible mess of error messages, a CV mismatch link that seemed to allow me to completely solve this problem.
The following code will appear to compile and run. I tried to make the formatting legible (without side scrolling) - I hope the code below is clear:
namespace boost{ namespace serialization{ template< class Archive, class S, int Rows_, int Cols_, int Ops_, int MaxRows_, int MaxCols_> inline void save( Archive & ar, const Eigen::Matrix<S, Rows_, Cols_, Ops_, MaxRows_, MaxCols_> & g, const unsigned int version) { int rows = g.rows(); int cols = g.cols(); ar & rows; ar & cols; ar & boost::serialization::make_array(g.data(), rows * cols); } template< class Archive, class S, int Rows_, int Cols_, int Ops_, int MaxRows_, int MaxCols_> inline void load( Archive & ar, Eigen::Matrix<S, Rows_, Cols_, Ops_, MaxRows_, MaxCols_> & g, const unsigned int version) { int rows, cols; ar & rows; ar & cols; g.resize(rows, cols); ar & boost::serialization::make_array(g.data(), rows * cols); } template< class Archive, class S, int Rows_, int Cols_, int Ops_, int MaxRows_, int MaxCols_> inline void serialize( Archive & ar, Eigen::Matrix<scalar, Rows_, Cols_, Ops_, MaxRows_, MaxCols_> & g, const unsigned int version) { split_free(ar, g, version); } }
A few critical points over the above code:
This code now has template parameters for all parameters of its own matrix. This should allow it to work with all types of matrices and vectors, regardless of whether they are specified at compile time or at run time. This is a significant improvement over the code above.
It is important that the serialization code is divided into separate save and load functions. Otherwise, the deserialization code will not resize the matrix to store the original data. I believe that Boost :: Serialize provides some additional features that can be overloaded to perform serialization or deserialization operations, but this approach was easier to implement.
The const qualifier in the save method is important. This was the source of my problems before the obscure g ++ error misled me.
I can not say that I have fully verified this code. If you (or anyone else) find more problems with him, let me know and I will try to keep track of anything else that I find.
Trust this helps.
Shmuel