How to Boost::serialize a hashmap of vectors?

I have a core data structure that I am loading values into: it is a hashmap of vectors. The vectors contain a struct, however. Further, the struct uses a template type.

I need to serialize this data structure and save to the disk periodically. Then, later--in a different program--I need to load the serialized data structure.

Here is a streamlined version of the structs. I minimally define them, but there are other data items (members), in addition to this bare bones version.

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

I started with the first struct, DataUnit and modified it as follows:

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

Eventually, I need to serialize the data structure. However, when I run just this with the following code:

int main(){
    DataUnit<int> hi;
    hi.time = time(NULL);
    hi.transaction = 1;
    hi.transaction_string = "world";
    return 0;
}

The world blows up with errors from boost. As far as I can tell, I followed the tutorial example exactly. How do I boost-serialize these objects?

Some of the errors (but there are so many I can't believe it isn't something fundamental...):

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()'

and it goes on from there...but I don't see where I have lacked any includes...(boost was installed via Cygwin)...

(running the code as an administrator...the text file I am outputting is present, with read write permissions...the ofs object is being created successfully)...


Currently, totally out of ideas... (tried linking lboost_serialization, reinstalling boost) No idea if I am missing something from the code ^^^


The problem is the order of your dependencies on the build command like. You need to list dependencies after the modules that use them. Also you don't compile .h files. They should be included in the .cpp files that use them. Try this command:

g++ -std=c++11 main.cpp hashmap_transaction.cpp -o run.exe  -lboost_serialization
链接地址: http://www.djcxy.com/p/45666.html

上一篇: 如何在PHP中检查上传文件的文件类型?

下一篇: 如何Boost ::序列化矢量散列表?