【发布时间】:2011-04-03 05:32:44
【问题描述】:
我有以下情况(简体):
啊哈:
#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;
}
现在,问题是我必须在保存函数中包含我想要序列化的所有类的头文件(在这种情况下,在 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
【问题讨论】:
-
btw - 不要将
using namespace std;和using namespace boost;放在头文件中 - 您可以将其放在“cxx”文件中,然后根据需要使用 std:: 限定符头文件。 -
@quamrana:谢谢你的建议;但我只是将这个例子拼凑起来,以说明我遇到的问题。对此有什么想法吗? :P
标签: c++ serialization boost include