c++ - 如何使用 boost C++ 反序列化对和对象的 vector

标签 c++ c++11 serialization boost

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


序列化过程:

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

  • 我要序列化的 vector 如下:vector<pair<boost::shared_ptr<QueryEntity>, string >>
  • 我对序列化过程没有问题
  • 问题出在反序列化过程中

编译日志

||=== 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]’|

ma​​in.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 stackoverflow. 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 需要在反序列化过程中使用默认构造函数。

关于c++ - 如何使用 boost C++ 反序列化对和对象的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29172014/

相关文章:

c# - C++/CX:将 std::string 转换为 Platform::String^

c++ - viewrich.cpp 中的调试断言错误

c++ - 在 C++ 中将多个模板类型传递给运算符<<

c++ - 为什么这个 SFINAE 在 gcc 中会报错?

python - 如何在 python 中读取/理解 PHP 序列化数据字符串

java - Hazelcast ReplicatedMap 序列化到文件中

c++ - 从资源加载图像不起作用(C++ Dll)

c++ - 如何从另一个类(class)关闭主窗口?

c++ - std::atomic 到什么程度?

python - 确定性 key 序列化