BGL反序列化的另一个问题

Yet another issue with BGL Deserialization

本文关键字:问题 另一个 反序列化 BGL      更新时间:2023-10-16

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

#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;
}
///////

EDIT是的,我将我的源代码链接到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})

由于序列化方法是一个模板,所以它应该放在。h文件中,而不是放在。cpp部分。