c++ - 使用 boost 序列化多态类
问题描述
当我尝试对 Sphere 是几何子类的数据进行序列化时,程序会因未处理的异常而崩溃。
在 Serialization.exe 中的 0x000007FEFCA0B87D 处引发异常:Microsoft C++ 异常:内存位置 0x000000000022F730 处的 boost::archive::archive_exception
这发生在“ar & g ;”这一行
include "pch.h"
#include <iostream>
#include<fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "Geometry.h"
#include "Sphere.h"
#include <boost/serialization/export.hpp>
int main()
{
const char* fileName = "saved.txt";
Sphere Sph;
Geometry* g = &Sph;
// save data
{
// Create an output archive
std::ofstream ofs(fileName);
boost::archive::text_oarchive ar(ofs);
ar & g ; // This lines throws exception.
}
Geometry* c_Restored;
//load data
{
// create an input stream
std::ifstream ifs(fileName);
boost::archive::text_iarchive ar(ifs);
ar & c_Restored;
}
c_Restored->PrintGeom();
do
{
std::cout << '\n' << "Press a key to continue...";
} while (std::cin.get() != '\n');
}
///////////////////////////////////////// //////////////////
#include "Geometry.h"
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/export.hpp>
class Sphere : public Geometry
{
private:
std::string stdstrSphere;
public:
Sphere() : stdstrSphere( "DefaultSphere"){}
Sphere( std::string str) : stdstrSphere(str) {}
void PrintGeom()
{
std::cout << "Sphere Virtual Function" << std::endl;
}
private:
typedef Geometry _Super;
friend class boost::serialization::access;
template <typename Archive>
void save(Archive& ar, const unsigned version) const {
boost::serialization::base_object<_Super>(*this);
ar & stdstrSphere;
}
template <typename Archive>
void load(Archive& ar, const unsigned version) {
boost::serialization::base_object<_Super>(*this);
ar & stdstrSphere;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
///////////////////////////////////////// ////////////////////////#pragma once
#include <string>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/base_object.hpp>
class Geometry
{
private:
std::string stdstringGeom;
public:
virtual void PrintGeom()
{
std::cout << "geometry virtual function";
}
private:
friend class boost::serialization::access;
template <typename Archive>
void save(Archive& ar, const unsigned version) const {
ar & stdstringGeom;
}
template <typename Archive>
void load(Archive& ar, const unsigned version) {
ar & stdstringGeom;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
解决方案
When I compile and run the code shown the exception text is...
unregistered class - derived class not registered or exported
So the underlying problem is that you need to register your derived class(es) as outlined here. Try adding the following just before main
...
BOOST_CLASS_EXPORT_GUID(Sphere, "Sphere")
推荐阅读
- ag-grid-react - 树模式下的 AG-Grid 页脚未更新
- python - Python:是否有类似 matlab 的反斜杠运算符?
- entity-framework - 如何创建 Group By LINQ 查询
- tensorflow - LSTM 的梯度一对多模型输出 wrt 输入
- java - spring AnnotationUtils in 5.2 不再搜索父类?
- ruby-on-rails - HelpScout Mailbox API 2.0 - 如何创建从“用户”到“客户”的传出对话
- c# - Gmail uses its sending account address as the reply-to, rather than person filling out the inquiry form
- c - Why is my pointer array returning a character in unicode?
- python-3.x - Python3. How to optimize decorator for handling exceptions?
- search - Using sed to find a string and replace that line but ignore commented out lines