我试着编写一个通用的序列化函数,它接受任何密集矩阵并将其序列化:其他一些有帮助但不是最终的问题 在这里: Question1 Question2
我尝试了以下哪些应该有效:
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());
}
}; // namespace serialization
}; // namespace boost
Run Code Online (Sandbox Code Playgroud)
当我尝试序列化时 Eigen::Matrix<double,4,4>
Eigen::Matrix<double,4,4> a;
boost::serialize(ar, a);
Run Code Online (Sandbox Code Playgroud)
编译器可能以某种方式与上面的模板不匹配?并给出以下错误:
/usr/local/include/boost/serialization/access.hpp|118|error:'class Eigen :: Matrix'没有名为'serialize'的成员
我正在尝试序列化Eigen的矩阵.这样我就可以序列化一个更复杂的对象.我使用Matrix作为基类,并在派生类中包含序列化.我很困惑如何处理Matrix.data(),它返回一个c风格的数组(如果我是正确的).这是我的尝试:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
template < class TEigenMatrix>
class VariableType : public TEigenMatrix {
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & this.data();
}
public:
};
Run Code Online (Sandbox Code Playgroud)
我想用它作为"包装器":
VariableType<Matrix<double,3,1>> serializableVector;
Run Code Online (Sandbox Code Playgroud)
代替
Matrix<double,3,1> vector;
Run Code Online (Sandbox Code Playgroud)