我有以下情况(简化):
a.h:
#include <boost/serialisation/serialisation.hpp>
class B;
using namespace std; using namespace boost;
class A {
B *b;
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & b; }
};b.h:
#include <boost/serialisation/serialisation.hpp>
class C;
using namespace std; using namespace boost;
class B {
C *c;
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & c; }
};c.h:
#include <boost/serialisation/serialisation.hpp>
using namespace std; using namespace boost;
class C {
int somevar;
template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & somevar; }
};main.cxx:
#include <boost/archive/text_oarchive.hpp>
#include <fstream>
#include "a.h"
#include "b.h"
#include "c.h"
using namespace std; using namespace boost;
int main() {
A a; // Create the object
ofstream ofs("save");
archive::text_oarchive oa(ofs);
oa << a; // Save the object
return 0;
}现在,问题是我必须在have函数中包含我想序列化的所有类的头文件(在本例中就在main中)。这样做的问题是,有比三个类多得多的类,而且它们更复杂,导致在这一点上存在编译瓶颈。
我最近才开始使用boost序列化,但我已经查看了文档,并在google和here上进行了搜索,所以我想我遗漏了一些明显的东西。
有没有人有解决这个问题的办法,只需要包含"a.h“,而不包括"b.h”、"c.h“等?
编辑:如果我注释掉#include "b.h“和"c.h”行,这可能是编译错误的关键部分:
main.cxx:17:1: instantiated from here
/usr/include/boost/type_traits/is_abstract.hpp:72:4: error: incomplete type ‘B’ not allowed发布于 2011-04-03 14:15:22
我认为您可以使用显式实例化,然后在.cpp文件中定义序列化成员函数。然后,a.cpp可以根据需要包括b.h和c.h,而a.h的用户不再需要这样做。
查看http://www.boost.org/doc/libs/1_46_1/libs/serialization/example/demo_pimpl_A.hpp (标头)和http://www.boost.org/doc/libs/1_46_1/libs/serialization/example/demo_pimpl_A.cpp (源文件)示例了解如何做到这一点。
发布于 2011-04-03 15:42:26
免责声明:我对boost序列化一无所知。
因为你有模板成员函数serialize,它必须有内联定义,使用成员,你应该#include这些成员的头文件,而不是仅仅声明例如class B;。
这意味着main只需在main中执行#include "a.h"操作,不会出现编译器错误。
发布于 2011-04-03 17:17:00
首先,如果有一个方法,任何需要访问A,B和C的方法,除了使A,B和C对该方法可见之外,没有其他方法,在你的情况下,这意味着包括所有3个头文件。所以你所说的编译器僵尸问题是无法解决的。
其次,您应该能够将非侵入式函数用于Boost的序列化。不确定这是否解决了您的特定问题,但它绝对值得一试,它确实将所有与序列化相关的东西移动到一个头文件中。
//header myserialization.h
#include "a.h"
#include "b.h"
#include "c.h"
template<class Archive>
void serialize(Archive & ar, const unsigned int version, A& a) { ar & a.*b; }
template<class Archive>
void serialize(Archive & ar, const unsigned int version, B& b) { ar & b.*c; }
template<class Archive>
void serialize(Archive & ar, const unsigned int version, C& c) { ar & c.somevar; }https://stackoverflow.com/questions/5527793
复制相似问题