BGL反序列化的另一个问题

时间:2016-08-28 01:28:33

标签: c++ serialization boost-graph adjacency-list adjacency-list-model

我正在尝试以图表格式对数据进行序列化和反序列化。我的图表的定义如下:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/directed_graph.hpp>
#include <boost/graph/adj_list_serialize.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/binary_object.hpp>

class VFull {
public:
    VFull();
    unsigned int id, hash, year;
    std::string ip, organization;
    VFull(unsigned int id, unsigned int hash, unsigned int year, std::string ip, std::string organization);
    template<class Archive> void serialize(Archive & ar, const unsigned int version);
};

class EFull {
public:
    EFull();
    template<class Archive> void serialize(Archive & ar, const unsigned int version);
};

class GFull {
public:
    template<class Archive> void serialize(Archive & ar, const unsigned int version);
};


//Defining the graph data structure. Using the vecS specification
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,VFull,EFull,GFull> graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor result_vertex_descriptor;
typedef boost::graph_traits<graph_t>::edge_descriptor result_edge_descriptor;

在我看来,我在序列化过程中没有问题,而我在反序列化部分有一些问题。事实上,在将这段代码添加到我的源代码之前,我没有链接错误:

#include <iomanip>
#include <iostream>
#include <fstream>
{
    std::ifstream file(path,std::ios_base::binary);
    boost::archive::binary_iarchive store{file};
    store >> graph; // Deserialization part that triggers the error
}

我试着在这里阅读一些问题并阅读Boost手册,但它们并没有解决我的问题。特别是,我有这个错误:

Undefined symbols for architecture x86_64:
  "void EFull::serialize<boost::archive::binary_iarchive>(boost::archive::binary_iarchive&, unsigned int)", referenced from:
      void boost::serialization::access::serialize<boost::archive::binary_iarchive, EFull>(boost::archive::binary_iarchive&, EFull&, unsigned int) in boostOthers.cpp.o
  "void GFull::serialize<boost::archive::binary_iarchive>(boost::archive::binary_iarchive&, unsigned int)", referenced from:
      void boost::serialization::access::serialize<boost::archive::binary_iarchive, GFull>(boost::archive::binary_iarchive&, GFull&, unsigned int) in boostOthers.cpp.o
  "void VFull::serialize<boost::archive::binary_iarchive>(boost::archive::binary_iarchive&, unsigned int)", referenced from:
      void boost::serialization::access::serialize<boost::archive::binary_iarchive, VFull>(boost::archive::binary_iarchive&, VFull&, unsigned int) in boostOthers.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

顺便说一下,我正在序列化和反序列化字符串和无符号整数,“本机数据类型”似乎得到了boost的支持:

//VERTICES
VFull::VFull() { }
VFull::VFull(unsigned int i, unsigned int h, unsigned int y, std::string p, std::string o) :
    id{i}, hash{h}, year{y}, ip{p}, organization{o} {

}
template<class Archive> void VFull::serialize(Archive &ar, const unsigned int version) {
    ar & 'i'; ar &  id;
    ar &  'h'; ar & hash;
    ar & '1'; ar & ip;
    ar & '2'; ar & organization;
    ar & '3'; ar & year;
    ar & '/';
}
///////


//EDGES
EFull::EFull() { }
template<class Archive> void EFull::serialize(Archive &ar, const unsigned int version) {
    char waste = 'e';
    ar & waste; }
///////


//GRAPH SIGNATURE
template<class Archive> void GFull::serialize(Archive &ar, const unsigned int version) {
    std::string waste = "GBLS";
    ar & waste;
}
///////

编辑是的,我将源代码链接到BGL和Boost的序列化。这是通过CMake脚本的这一部分实现的:

find_package( Boost REQUIRED COMPONENTS graph serialization )
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED OFF)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} ..some other stuff..)
TARGET_LINK_LIBRARIES(mycode ${Boost_LIBRARIES})

1 个答案:

答案 0 :(得分:0)

由于序列化方法是模板,因此应将其放在.h文件中,而不是放在.cpp部分中。