【发布时间】:2018-01-25 13:54:27
【问题描述】:
我想对使用 BGL 存储的图形进行磁盘 I/O。我正在使用 boost::serialization。
首先,一些可以编译的代码:
typedef boost::adjacency_list<
boost::vecS
,boost::vecS
,boost::undirectedS
> graph_t;
int main()
{
graph_t g;
std::ifstream ifs( "file_in" ); // read from file
boost::archive::text_iarchive ia( ifs );
ia >> g;
std::ofstream ofs( "file_out" ); // save to file
boost::archive::text_oarchive oa( ofs );
oa << g;
}
现在,我需要将数据存储到我的顶点中。所以我重新定义了我的图表:
struct myVertex
{
int a;
float b;
};
typedef boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS,
myVertex
> graph_t;
当然,我需要定义如何序列化myVertex。由于我不想混淆该数据结构,因此我想使用非侵入式方式,因为它是described in the manual。
因此,按照手册中的说明,我添加了所需的功能:
namespace boost {
namespace serialization {
template<class Archive>
void serialize( Archive& ar, const myVertex& mv, const unsigned int version )
{
ar & mv.a;
ar & mv.b;
}
} // namespace serialization
} // namespace boost
不幸的是,这不能编译:编译器抱怨缺少序列化函数:
错误:‘struct myVertex’没有名为‘serialize’的成员
我对此的理解是,内部 BGL 数据结构提供了一个序列化函数,它本身依赖于顶点(显然还有边)的类成员序列化函数。并且它不能使用外部序列化函数。
请注意,如果我使用所谓的“侵入式”方式(将序列化函数添加为类成员),它确实构建得很好,但我想知道它是否可以按照上面的说明完成.
【问题讨论】:
标签: c++ serialization boost boost-graph