升压序列多态寄存器(出口)跨文件不工作 [英] Boost serialization polymorphic register(export) not working across files

查看:150
本文介绍了升压序列多态寄存器(出口)跨文件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的boost ::系列化在我的项目。该项目是大的,并且在多个地方进行序列化我的对象。按照文档这里,我要导出我的类两地分居的一步。

I am using boost::serialization in my project. The project is large, and serializes my objects in several places. According to the documentation here, I should export my class with two separated step.


  1. BOOST_EXPORT_KEY() .H 文件,巫包含声明。

  2. BOOST_EXPOET_IMPLEMENT()的.cpp 文件,巫包含了出口的实例(定义)。

  1. BOOST_EXPORT_KEY() in .h file, witch contains the declaration.
  2. BOOST_EXPOET_IMPLEMENT() in .cpp file, witch contains the instantiation(definition) of the exporting.

hier.h 的类层次结构,有3类层次结构中的。

hier.h the class hierarchy, there are 3 classes in the hierarchy.

/*
B <---+--- D1
      |
      +--- D2
*/

#include <boost/serialization/base_object.hpp>                                                                                                                                                                 

class B {                                                                                                                                                                                                      
public:                                                                                                                                                                                                        
    virtual ~B() {}                                                                                                                                                                                            
    template < typename Ar >                                                                                                                                                                                   
    void serialize(Ar& ar, const int) {                                                                                                                                                                        
    }                                                                                                                                                                                                          
} ;                                                                                                                                                                                                            

class D1 : public B {                                                                                                                                                                                          
public:                                                                                                                                                                                                        
    virtual ~D1() {}                                                                                                                                                                                           
    template < typename Ar > void serialize(Ar& ar, const int) {                                                                                                                                               
        boost::serialization::base_object<B>(*this);                                                                                                                                                           
    }                                                                                                                                                                                                          
} ;                                                                                                                                                                                                            

class D2 : public B {                                                                                                                                                                                          
public:                                                                                                                                                                                                        
    template < typename Ar > void serialize(Ar& ar, const int) {                                                                                                                                               
        boost::serialization::base_object<B>(*this);                                                                                                                                                           
    }                                                                                                                                                                                                          
    virtual ~D2() {}                                                                                                                                                                                           
} ;                                                                                                                                                                                                            

#include <boost/serialization/export.hpp>                                                                                                                                                                      

BOOST_CLASS_EXPORT_KEY(B);                                                                                                                                                                                     
BOOST_CLASS_EXPORT_KEY(D1);                                                                                                                                                                                    
BOOST_CLASS_EXPORT_KEY(D2);

和一个 hier.cpp 包含实现:

#include <boost/serialization/export.hpp>
#include "hier.h"

BOOST_CLASS_EXPORT_IMPLEMENT(D1);
BOOST_CLASS_EXPORT_IMPLEMENT(D2);

和一个的main.cpp 使用序列化:

#include <iostream>                                                                                                                                                                                            
#include <sstream>                                                                                                                                                                                             
#include <boost/archive/text_iarchive.hpp>                                                                                                                                                                     
#include <boost/archive/text_oarchive.hpp>                                                                                                                                                                     
#include <boost/serialization/export.hpp>                                                                                                                                                                      
#include "hier.h"                                                                                                                                                                                              

int main(int argc, char* argv[])                                                                                                                                                                               
{                                                                                                                                                                                                              
    B* d1 = new D1();                                                                                                                                                                                          
    B* d2 = new D2();                                                                                                                                                                                          
    std::ostringstream os;                                                                                                                                                                                     
    boost::archive::text_oarchive oa (os);                                                                                                                                                                     
    oa & d1 & d2;                                                                                                                                                                                              
}

它编译没有任何问题,但运行会引起:

It compiled without any problem, but run it will cause:

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  unregistered class - derived class not registered or exported

这意味着派生类没有注册,是指在 hier.cpp 不正常的注册。但是,这真的很奇怪,因为:

Which means the derived class is not registered, means the registration in the hier.cpp is not working. But that is really strange, because:


  1. 如果我注册的实施既的main.cpp hier.cpp ,它发出重复定义而连接。 表示在注册 hier.cpp 是OK,并且暴露到接头的知名度。,否则会出现没有重复定义错误。

  1. If I register implementation is both main.cpp and hier.cpp, it issue duplicated definition while linking. Means the registration in hier.cpp is OK and is exposed into the linkers visibility., otherwise there will be no duplicated definition error.

如果我只登记在的main.cpp 的实现,它运行正常。

If I register implementation only in main.cpp, it runs OK.

我在那种情况下真的很困惑。任何意见和建议是AP preciated。提前致谢。

I am really confused in that situation. Any comment and suggestion is appreciated. Thanks in advance.

推荐答案

在调用之前 BOOST_CLASS_EXPORT _ * 你应该包括你要使用的档案。在万客隆然后添加特定的序列化,功能的标头。

Before calling BOOST_CLASS_EXPORT_* you should include the archives which you want to use. The makro then adds specific serialize-functions for the headers.

这意味着你应该在 hier.cpp 更改code以下内容:

This means you should change your code in hier.cpp to the following:

#include <boost/serialization/export.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "hier.h"

BOOST_CLASS_EXPORT_IMPLEMENT(D1);
BOOST_CLASS_EXPORT_IMPLEMENT(D2);

在code在 hier.h 相应的变化:

#include <boost/serialization/export.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

BOOST_CLASS_EXPORT_KEY(B);
BOOST_CLASS_EXPORT_KEY(D1);
BOOST_CLASS_EXPORT_KEY(D2);

来源:结果
提升Serializsation文档

PS:结果
我不知道这是否是解决你的问题,但我认为这可能会造成一些麻烦。我认为它值得一试。

PS:
I do not know if this is solving your problem, but I think it could be causing some trouble. I think its worth a try.

这篇关于升压序列多态寄存器(出口)跨文件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆