부스트 serialize를 사용하여 파생 클래스에 대한 포인터를 비 직렬화하려는 이상한 문제가 있습니다. 내가베이스 및 파생 된 /로드 함수를 외부 (비 관입 버전) 있지만 포인터를 deserialize 할 때마다 "입력 스트림 오류"예외 또는 "등록되지 않은 클래스"예외가 얻을 때마다 파생됩니다.부스트를 사용하여 파생 클래스에 대한 포인터를 비 직렬화하는 문제 : serialize
먼저 내 클래스를 정의 :
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)
다음 내가 실제 저장 /로드 기능을 정의
#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()
{}
그럼 내가 그들의 특성은 내가 필요한 있는지 확인 여기에 내가 할 것입니다 :
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
그리고이 나는 문서
에서 차용로드 도우미/저장 마지막으로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");
}
내가 뭘 잘못 사용하려고 어떻게? 기본 클래스에 대한 포인터 이외의 다른 요소를 deserialize 할 수없는 이유는 무엇입니까?
문제를 해결 한 경우 솔루션을 _answer_으로 게시하십시오. –
그걸 위해, 나는 이것을 어떻게 해야할지 찾아야합니다 :) 미안 해요, 전 완전히 새로운 것입니다. 추신 다음 4 시간 동안 답변으로 게시 할 수없는 것 같습니다. 사이트 제한 – Zeks
결국 해결책이 아닙니까? 문제가 해결되었다고하셨습니까? –