问题描述
我使用的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.
-
BOOST_EXPORT_KEY()
在.H
文件,巫包含声明。 -
BOOST_EXPOET_IMPLEMENT()
在的.cpp
文件,巫包含了出口的实例(定义)。
BOOST_EXPORT_KEY()
in.h
file, witch contains the declaration.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:
-
如果我注册的实施既
的main.cpp
和hier.cpp
,它发出重复定义而连接。 表示在注册hier.cpp
是OK,并且暴露到接头的知名度。,否则会出现没有重复定义错误。
If I register implementation is both
main.cpp
andhier.cpp
, it issue duplicated definition while linking. Means the registration inhier.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);
来源:结果
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.
这篇关于升压序列多态寄存器(出口)跨文件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!