/ / boost :: serializationを使用して属性としてオブジェクトのベクトルを持つオブジェクトを直列化する方法 - C ++、オブジェクト、シリアライゼーション、ブースト、ベクトル

boost :: serializationを使って属性としてオブジェクトのベクトルを持つオブジェクトを直列化する方法 - C ++、オブジェクト、直列化、ブースト、ベクトル

私はオブジェクトの基底を作成しようとしています。これには、異なるクラスのオブジェクトの属性ベクトルとして含まれています。

    #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_0boostserializationaccess.hpp(118):エラーC2039:serialize: "std :: vector>"のメンバーではありません。

そのタイプのオブジェクトをシリアライズする方法の経験を共有できますか?

回答:

回答№1は1

コードをコンパイル可能にするには、以下を実行する必要があります。

  1. ベクトルの直列化を担当するヘッダーを含める

    #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)
    {
    // ...
    }
    // ...
    };
    

ここに コンパイル可能なコードです。