我想序列化一个 boost::array,其中包含一些已经可以序列化的东西。
如果出现这个错误:
error C2039: 'serialize' : is not a member of 'boost::array<T,N>'
我试图包含 serialization/array.hpp header ,但没有帮助。 是否有另一个 header 要包含?
谢谢
编辑: 删除了一个错误的链接
最佳答案
您需要显示包含在 boost::array 中的类的代码。由于 boost::array 是 STL-compliant ,应该没有理由说这行不通。你应该做类似 this 中的 bus_route 和 bus_stop 类的事情。示例。
boost::array 中包含的类必须将 boost::serialization::access 声明为友元类并实现如下的 serialize 方法:
class bus_stop
{
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const bus_stop &gp);
virtual std::string description() const = 0;
gps_position latitude;
gps_position longitude;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & latitude;
ar & longitude;
}
protected:
bus_stop(const gps_position & _lat, const gps_position & _long) :
latitude(_lat), longitude(_long)
{}
public:
bus_stop(){}
virtual ~bus_stop(){}
};
一旦完成,std 容器应该能够序列化 bus_stop:
class bus_route
{
friend class boost::serialization::access;
friend std::ostream & operator<<(std::ostream &os, const bus_route &br);
typedef bus_stop * bus_stop_pointer;
std::list<bus_stop_pointer> stops;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
// in this program, these classes are never serialized directly but rather
// through a pointer to the base class bus_stop. So we need a way to be
// sure that the archive contains information about these derived classes.
//ar.template register_type<bus_stop_corner>();
ar.register_type(static_cast<bus_stop_corner *>(NULL));
//ar.template register_type<bus_stop_destination>();
ar.register_type(static_cast<bus_stop_destination *>(NULL));
// serialization of stl collections is already defined
// in the header
ar & stops;
}
public:
bus_route(){}
void append(bus_stop *_bs)
{
stops.insert(stops.end(), _bs);
}
};
注意重要的一行:
ar & stops;
这将自动遍历 std 容器,在本例中为 std::list bus_stop 指针。
错误:
error C2039: 'serialize' : is not a member of 'boost::array<T,N>'
表示包含在 boost::array 中的类要么没有将 boost::serialization::access 声明为友元类,要么没有实现模板方法 serialize。
关于c++ - 序列化boost数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3045188/