【问题标题】:Use Boost serialization with forward declaration class and inheritance使用带有前向声明类和继承的 Boost 序列化
【发布时间】:2016-11-17 11:56:22
【问题描述】:

我正在使用 boost 序列化来存储和加载多个类。我的目标是在一个包含其他类的类上使用序列化,因此其他类将被序列化。

但问题是这些类包含前向声明和继承,我无法使用这些类进行 boost 序列化。

我在编译时遇到问题,特别是在前向声明时,出现以下错误:

error: invalid use of incomplete type ‘class C’
error: forward declaration of ‘class C’
error: ‘value’ is not a member of ‘boost::is_polymorphic<C>’
...

谁能告诉我我的代码有什么问题?我错过了什么吗? 我用于序列化派生和转发声明的类的代码是否正确?

A.h:

#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>

class B;
class C;

class A {

private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & m_Bvector;
    }

protected:
    vector<B*> m_Bvector;
    vector<C*> m_Cvector;

/*....*/

}

注意:向量 m_Bvector 可以包含 B* 或/和 C* 对象

B.h:

#include <boost/serialization/access.hpp>
#include "A.h"

class B {

private : 
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & m_someInt;
    }

protected :
    int m_someInt;

/*....*/

}

C.h:

#include <boost/serialization/base_object.hpp>
#include "B.h"

classe C : public B {

private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & boost::serialization::base_object<B>(*this);
        ar & m_someOtherInt;
    }

protected:
     int m_someOtherInt;

/*....*/

}

这是我调用保存和加载函数的方法:

SerializationManager.h:

#include <A.h>
#include <C.h>
#include <boost/serialization/export.h>

BOOST_CLASS_EXPORT(C);

class SerializationManager {

    /*....*/

public:
    bool save(string filename);
    bool load(string filename);

protected:
    A* m_classToSave;

}

SerializationManager.cpp:

#include "SerializationManager.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <fstream>
#include <sstream>

bool SerializationManager::save(string filemname)
{

    std::ofstream outputFile(filemname);
    assert(outputFile.good());
    boost::archive::text_oarchive oTextArchive(outputFile);

    oTextArchive << m_classToSave;

    return true;
}

bool SerializationManager::load(string filename)
{
    delete m_classToSave;

    std::ifstream ifs( filename );
    assert(ifs.good());
    boost::archive::text_iarchive ia(ifs);

    // restore the schedule from the archive
    ia >> m_classToSave;

    return true;
}

/* ... */

【问题讨论】:

    标签: c++ inheritance serialization boost


    【解决方案1】:

    Boost 需要知道类型是否是虚拟的(具有任何虚拟方法,即通常使用 vtable 实现),因此它可以依赖 typeiddynamic_cast 来返回运行时保真值。

    您试图在类型定义可用之前实例化序列化机制(仅前向声明时类型不完整),因此它无法生成序列化代码。

    【讨论】:

    • 我弄明白了为什么 boost 不能用我写的东西生成序列化代码。但是我尝试了几种方法来完成我的工作,但我没有这样做。你有解决方案让我的代码工作吗?
    • 欢迎来到stackoverflow。别忘了vote
    • 啊。评论中添加了一个问题。我稍后看看
    • 同时这里有十几个答案展示了如何使用 boost 序列化导出类。我喜欢提供完整的现场样品,所以会有一些可以看stackoverflow.com/…
    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多