c++ - 如何使用 boost::serialization 序列化对象 vector 作为属性的对象

标签 c++ object serialization boost vector

我正在尝试创建对象的基础,其中包含来自不同类的对象的属性 vector 。这是我的代码:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include <boost/archive/binary_oarchive.hpp>
    #include <boost/archive/binary_iarchive.hpp>

    using namespace std;

    class form_mesto
    {
    public:
        string mesto;
        int year;
        int mounth;
        int day;
        bool visit = false;
        form_mesto(string a_mesto)
        {
            mesto = a_mesto;
        }
    };

    class Place
    {
    private:
        friend class boost::serialization::access;
        template<class Archieve>
        void serialize(Archieve&ar, const unsigned int version)
        {
            ar& mestа;
            ar& person;
        }
    public:
        string person;
        vector<form_mesto> mestа;

        Place(string a_person)
        {
            person = a_person;
        }

        void add_place(form_mesto a_mesto)
        {
            mestа.push_back(a_mesto);
        }
    };

int main()
{
    string input_in_form = "London";
    string input_in_Place = "Eugene";
    form_mesto z = form_mesto(input_in_form);
    Place x = Place(input_in_Place);
    x.add_place(z);
    std::ofstream ofs("save.dat", std::ios::binary);
    boost::archive::binary_oarchive oa(ofs);
    oa<<x;
};

错误,我得到的是:

c:\boost_1_57_0\boost\serialization\access.hpp(118):错误 C2039:序列化:不是“std::vector>”的成员。

有人可以分享如何序列化该类型对象的经验吗?

最佳答案

要使代码可编译,必须执行以下操作:

  1. 包含负责 vector 序列化的 header

    #include <boost/serialization/vector.hpp>
    
  2. serialize方法添加到form_mesto

    class form_mesto
    {
    private:
        friend class boost::serialization::access;
        template<class Archieve>
        void serialize(Archieve&ar, const unsigned int version)
        {
            // ...
        }
    // ...
    };
    

Here是可编译代码。

关于c++ - 如何使用 boost::serialization 序列化对象 vector 作为属性的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28752246/

相关文章:

c++ - 双数组 Trie 问题

iphone - 构建类名形成字符串

Python:有关 Python 函数实现的信息

java - 序列化 Spring BindingAwareModelMap

java - Hessian 类 SerializerFactory 是线程安全的吗?

c++ - 以类似方式访问数组中的结构成员 : padding in structs different than in arrays?

c++ - 我的c++代码。会不会出现内存泄漏?

php - 为什么不能在静态上下文中使用任何魔术常量?

Java 试图序列化不存在的东西?

c++ - QProcess::startDetached() 但隐藏控制台窗口