【发布时间】:2012-01-11 12:45:47
【问题描述】:
我在尝试使用 boost 序列化反序列化指向派生类的指针时遇到了一个奇怪的问题。我有一个基础,并在它们之外使用保存/加载功能(非侵入式版本)派生,但每次我尝试反序列化指针时,我都会得到“输入流错误”异常或“未注册类”异常。这是我的工作:
首先我定义我的类:
#include <fstream>
#include <iomanip>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/archive_exception.hpp>
#include "boost/serialization/split_free.hpp"
#include "boost/serialization/export.hpp"
#include "boost/serialization/utility.hpp"
#include <boost/serialization/string.hpp>
#include <boost/serialization/binary_object.hpp>
class Base
{
public:
bool isEnabled;
Base();
virtual ~Base(){}
};
Base::Base()
{
isEnabled = 0;
}
class Derived : public Base
{
public:
Derived();
virtual ~Derived(){}
int layerHeight;
};
Derived::Derived():Base()
{}
然后我确保他们的特征是我需要的:
BOOST_CLASS_EXPORT_GUID(Base, "Base")
BOOST_SERIALIZATION_SPLIT_FREE(Base)
BOOST_CLASS_IS_WRAPPER(Base)
BOOST_CLASS_TRACKING(Base, boost::serialization::track_selectively)
BOOST_CLASS_IMPLEMENTATION(Base, boost::serialization::object_class_info)
BOOST_SERIALIZATION_SPLIT_FREE(Derived)
BOOST_CLASS_EXPORT_GUID(Derived, "Derived")
BOOST_CLASS_IS_WRAPPER(Derived)
BOOST_CLASS_IMPLEMENTATION(Derived, boost::serialization::object_class_info)
BOOST_CLASS_TRACKING(Derived, boost::serialization::track_selectively)
接下来我定义实际的保存/加载函数:
namespace boost {
namespace serialization {
template<class Archive>
void save(Archive & ar,const Base& obj, const unsigned int version)
{
bool isEnabled = obj.isEnabled;
ar << BOOST_SERIALIZATION_NVP(isEnabled);
}
template<class Archive>
void load(Archive & ar, Base& obj, const unsigned int version)
{
bool isEnabled;
ar >> BOOST_SERIALIZATION_NVP(isEnabled);
}
} // namespace serialization
} // namespace boost
namespace boost {
template<>
struct is_virtual_base_of<Base, Derived>: public mpl::true_ {};
namespace serialization {
template<class Archive>
void save(Archive & ar,const Derived& obj, const unsigned int version)
{
ar & boost::serialization::base_object<Base>(obj);
int height =obj.layerHeight;
ar << BOOST_SERIALIZATION_NVP(height);
}
template<class Archive>
void load(Archive & ar, Derived& obj, const unsigned int version)
{
ar.template register_type<Base>();
ar.template register_type<Derived>();
ar & boost::serialization::base_object<Base>(obj);
int height;
ar >> BOOST_SERIALIZATION_NVP(height);
}
} // namespace serialization
} // namespace boost
还有 2 个我从 Docs 借来的保存/加载助手
template <typename T>
void save_schedule(const T& s, const char * filename){
// make an archive
std::ofstream ofs(filename);
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(s);
}
template <typename T>
void restore_schedule(T &s, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
assert(ifs.good());
boost::archive::xml_iarchive ia(ifs);
// restore the schedule from the archive
ia >> BOOST_SERIALIZATION_NVP(s);
}
最后 - 这是我尝试全部使用它的方法
int main(int argc, char *argv[])
{
Base* basePointer = new Base();
Base* objectPointer = new Derived();
Derived * secondObjectPointer = new Derived();
Derived justObject;
save_schedule(basePointer, "C:\\basePointer.xml");
save_schedule(objectPointer, "C:\\objectPointer.xml");
save_schedule(secondObjectPointer , "C:\\secondObjectPointer.xml");
save_schedule(justObject, "C:\\justObject.xml");
//this works OK
restore_schedule(basePointer, "C:\\basePointer.xml");
//this gives "Input Stream Error"
restore_schedule(objectPointer, "C:\\objectPointer.xml");
//this gives "Unregistered class"
restore_schedule(secondObjectPointer, "C:\\secondObjectPointer.xml");
//This works >__< But I need to serialize pointers so I cannot use this
restore_schedule(justObject, "C:\\justObject.xml");
}
我做错了什么?为什么我不能反序列化除了指向基类的指针之外的任何东西?
【问题讨论】:
-
如果您解决了问题,请将解决方案发布为答案。
-
为此,我需要找到如何做到这一点:) 对不起,我是全新的。附:在接下来的 4 小时内,我似乎无法将其作为答案发布 - 站点限制
-
那毕竟不是解决方案?你说问题解决了。
-
这是解决方案,我只是不知道如何发布任何内容作为答案而不是编辑我的帖子,现在事实证明这有 8 小时的限制。
-
哦,抱歉,我误解了您最初的评论。您的意思是您不知道如何在 SO 上发布答案;我以为你在谈论实施你的解决方案。是的,您必须等待 8 小时才能发布自我回答;不过,请这样做。我已将您的解决方案从问题中删除,因为它不属于那里。谢谢!
标签: c++ serialization boost