我有一个核心数据结构,我正在加载值:它是一个向量的散列图。然而,矢量包含一个结构。此外,该结构使用模板类型。如何Boost ::序列化向量的散列表?
我需要序列化这个数据结构并定期保存到磁盘。然后,在稍后 - 在不同的程序中 - 我需要加载序列化的数据结构。
下面是结构的简化版本。我最低限度地定义它们,但除了这个裸露的骨骼版本之外,还有其他数据项目(成员)。
#include<vector>
#include<string>
#include<map>
#include<fstream>
#include<stdlib.h>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_oarchive.hpp>
using namespace std;
template<typename T>
struct DataUnit{
size_t time;
string transaction_string;
T transaction;
}
template<typename T>
struct DataStructure{
map<string transaction_hash, vector<DataUnit<T>> > hashmap;
int max_transactions;
// I have a method to add stuff, but omitted for readability
}
我开始与所述第一结构,DataUnit
和改性,如下所示:
template<typename T>
struct DataUnit{
size_t time;
string transaction_string;
T transaction;
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
ar & time;
ar & transaction;
ar & transaction_string;
}
};
最后,我需要序列的数据结构。然而,当我用下面的代码运行这个:
int main(){
DataUnit<int> hi;
hi.time = time(NULL);
hi.transaction = 1;
hi.transaction_string = "world";
return 0;
}
世界爆炸与错误从助推。据我所知,我遵循教程示例exactly。如何提升序列化这些对象?
一些错误(但有这么多我不能相信这是不是根本的...):
In function `boost::archive::text_oarchive::text_oarchive(std::ostream&, unsigned int)
undefined reference to `boost::archive::text_oarchive_impl::text_oarchive_impl(std::ostream&, unsigned int)'
last error:
undefined reference to `boost::archive::archive_exception::~archive_exception()'
,并从那里继续......但我不t看到我缺少任何包括...(提升通过Cygwin安装)...
(以管理员身份运行代码...我输出的文本文件存在,具有读写权限..目标正在创建中)...
目前,完全没了主意...(试图连接lboost_serialization,重新安装增压)不知道如果我从代码^^^
我想你需要链接到一个boost库。这看起来像链接错误。 –
@KurtStutsman感谢的人。我试过了-lboost_serialization;但我很难搞清楚还有什么可以尝试的。你知道我可以去哪里寻找可能的联系清单/文件吗? – bordeo
你用来构建的命令是什么?你可以将整个命令行添加到问题中吗? –