Не могу сериализовать std::vector с Cereal

Я новичок в сериализации, и у меня возникли проблемы с сериализацией std::vector объекты с библиотекой зерновых. Вот пример, который иллюстрирует проблему:

class MyClass
{
    int x, y, z;

    class MyOtherClass
    {
        string name, description;

    public:

        template<class Archive>
        void serialize(Archive & archive)
        {
            archive(name, description);
        }
    };

    vector<MyOtherClass> Victor;
    vector<int> ints;

public: 

    template<class Archive>
    void serialize(Archive & archive)
    {
        archive(x, y, z, ints); // error C2338: cereal could not find any output serialization functions for the provided type and archive combination.
    }
};

Попытка сериализации либо ints объект или Victor результат объекта в error C2338: cereal could not find any output serialization functions for the provided type and archive combination.

Вот код, который я использую в main функция:

MyClass MyObject;
ofstream datafile(path, ios::binary);
{ cereal::BinaryOutputArchive oarchive(datafile); oarchive(MyObject); }

Что я делаю неправильно?

1 ответ

      #include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

#include <cereal/archives/json.hpp>
#include <cereal/types/vector.hpp> 
// See details in http://uscilab.github.io/cereal/stl_support.html

class MyClass {
  int x, y, z;

  class MyOtherClass {
    string name, description;

  public:
    template <class Archive>
    void serialize( Archive &archive )
    {
      archive( CEREAL_NVP( name ), CEREAL_NVP( description ) );
    }
  };

  vector<MyOtherClass> Vector;
  vector<int>          ints;

public:
  template <class Archive>
  void serialize( Archive &archive )
  {
    archive( CEREAL_NVP( x ), CEREAL_NVP( y ), CEREAL_NVP( z ), CEREAL_NVP( ints ) );
  }

  // Add one element to the private vector
  void populateVector( const int value ) {
    ints.push_back( value );
  }
};

int main()
{
  MyClass  MyObject{};

  MyObject.populateVector( 101 );
  MyObject.populateVector( 202 );
  MyObject.populateVector( 303 );

  // For brevity I just print the serialization to the standard output instead of the binary file
  cereal::JSONOutputArchive oarchive( cout );
  oarchive( MyObject );

  return 0;
}

Этот код должен выдать следующий вывод:

      {
    "value0": {
        "x": 0,
        "y": 0,
        "z": 0,
        "ints": [
            101,
            202,
            303
        ]
    }
}
Другие вопросы по тегам