Boost c ++序列化char * [英] Boost c++ serializing a char *
问题描述
我有一个类,我试图序列化一个shared_ptr,但序列化对象的正常方法不工作:
I have a class and I am trying to serialize a shared_ptr but the normal method of serializing an object is not working:
class Object {
public:
Object();
~Object();
shared_ptr<char>objectone;
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & objectone;
}
};
我已经尝试过这种方式,但它仍然不工作:
I've even attempted it in this way but it still doesn't work:
void serialize(Archive &ar, const unsigned int version)
{
for (int i = 0; i < (strlen(objectone.get())); i++)
ar & objectone.get()[i];
}
任何想法如何处理?非常感谢。
Any ideas how to approach this? Thanks.
一些额外的信息:
我已经包括了两个shared_ptr头文件:
I've already included both shared_ptr header files:
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/shared_ptr_132.hpp>
我试图转换为字符串并以这种方式序列化,但它会产生以下错误:
boost :: archive :: archive_exception'
what():stream error
I've attempted to convert to a string and serialize it in that way but it produces the following error: boost::archive::archive_exception' what(): stream error
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
if (objectone.get()) {
string temp(objectone.get());
ar & temp;
}
ar & objectone;
}
推荐答案
。
您可以执行
class Object {
public:
Object();
~Object();
std::string name;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & name;
}
};
完成。
strong>添加
Addition
根据您的要求,这是您必须做的
Given your requirements this is what you have to do
class test
{
public:
friend class boost::serialization::access;
test()
{
}
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
int len = strlen(data.get());
ar & len;
for (int index = 0; index < len; ++index)
ar & data[index];
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
int len = 0;
ar & len;
data = boost::shared_array<char>(new char[len+1]);
for (int index = 0; index < len; ++index)
ar & data[index];
data[len] = 0;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
boost::shared_array<char> data;
};
这篇关于Boost c ++序列化char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!