如何使用 boost C++ 反序列化对和对象的向量

How to deserialize a vector of pairs and objects using boost c++

我在使用 boost 库进行反序列化时遇到问题。


序列化过程:

序列化过程基于How can I serialize an std::vector with boost::serialization


编译日志

||=== Build: Debug in EntityLinking (compiler: GNU GCC Compiler) ===|

- access.hpp||In instantiation of ‘static void boost::serialization::access::construct(T*) [with T = QueryEntity]’:|
- serialization.hpp|93|required from ‘void boost::serialization::load_construct_data(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = QueryEntity]’|
- serialization.hpp|158|required from ‘void boost::serialization::load_construct_data_adl(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = QueryEntity]’|
- iserializer.hpp|323|required from ‘void boost::archive::detail::pointer_iserializer<Archive, T>::load_object_ptr(boost::archive::detail::basic_iarchive&, void*&, unsigned int) const [with Archive = boost::archive::text_iarchive; T = QueryEntity]’|
- iserializer.hpp|203|required from ‘class boost::archive::detail::pointer_iserializer<boost::archive::text_iarchive, QueryEntity>’|
- interface_iarchive.hpp|54|required from ‘const boost::archive::detail::basic_pointer_iserializer* boost::archive::detail::interface_iarchive<Archive>::register_type(T*) [with T = QueryEntity; Archive = boost::archive::text_iarchive]’|
- iserializer.hpp|459|  [ skipping 92 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]|
- common_iarchive.hpp|66|required from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = std::vector<std::pair<boost::shared_ptr<QueryEntity>, std::basic_string<char> > >; Archive = boost::archive::text_iarchive]’|
- basic_text_iarchive.hpp|65|required from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = std::vector<std::pair<boost::shared_ptr<QueryEntity>, std::basic_string<char> > >; Archive = boost::archive::text_iarchive]’|
- text_iarchive.hpp|82|required from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = std::vector<std::pair<boost::shared_ptr<QueryEntity>, std::basic_string<char> > >; Archive = boost::archive::text_iarchive]’|
- interface_iarchive.hpp|60|required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = std::vector<std::pair<boost::shared_ptr<QueryEntity>, std::basic_string<char> > >; Archive = boost::archive::text_iarchive]’|
- interface_iarchive.hpp|67|required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator&(T&) [with T = std::vector<std::pair<boost::shared_ptr<QueryEntity>, std::basic_string<char> > >; Archive = boost::archive::text_iarchive]’|

main.cpp|49|从这里需要|

- access.hpp|132|error: no matching function for call to ‘QueryEntity::QueryEntity()’|
- access.hpp|132|note: candidates are:|
- QueryEntity.h|28|note: QueryEntity::QueryEntity(const string&, const string&, std::vector<std::basic_string<char> >&)|
- QueryEntity.h|28|note:   candidate expects 3 arguments, 0 provided|
- QueryEntity.h|15|note: QueryEntity::QueryEntity(const QueryEntity&)|
- QueryEntity.h|15|note:   candidate expects 1 argument, 0 provided|
- QueryEntity.h|15|note: QueryEntity::QueryEntity(QueryEntity&&)|
- QueryEntity.h|15|note:   candidate expects 1 argument, 0 provided|

main.cpp

    #include <iostream>
    #include <fstream>
    
    // include input and output archivers
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/serialization/vector.hpp>
    //serialize shared_ptr
    #include <boost/serialization/shared_ptr.hpp>
    //serialize pair
    #include <boost/serialization/utility.hpp>
    
    #include "query/LoadQuery.h"
    using namespace std;
    
    int main()
    {
    
            LoadQuery lq("list_queries.txt");
            vector<pair<boost::shared_ptr<QueryEntity>, string >> queries = lq.createQuerties()
        //serialization
            {
                ofstream ofs("variations.var");
                boost::archive::text_oarchive oa(ofs);
                oa & queries;
            }
      vector<pair<boost::shared_ptr<QueryEntity>, string >> variations;
       {
         std::ifstream ifs("variations.var");
         boost::archive::text_iarchive ia(ifs);
         ia & variations;
       }
    return 0;
    }

查询实体

#ifndef QUERYENTITY_H
#define QUERYENTITY_H

#include <vector>
#include <string>

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;

class QueryEntity
{

public:
    QueryEntity(const string &, const string &, vector<string> &);
    //AFTER CORRECTION by Whosebug. I had to add a default constructor to the clas
    QueryEntity();
    string getId() const;
    string getDocId() const;
    vector<string> getName() const;
    void setVector(const vector<string> & v);
private:
    string _id;
    string _docId;
    vector<string> _name;
friend class boost::serialization::access;
    template<class Archive>

    void serialize(Archive & ar, const unsigned int version)
    {
        ar & _id;
        ar & _docId;
        ar & _name;
    }

};

#endif // QUERYENTITY_H

编译器给您一个错误,提示您没有默认构造函数。更有可能boost需要在反序列化过程中使用默认构造函数。