2

I'm new to serialization, and I'm having trouble serializing std::vector objects with the Cereal library. Here's a sample that illustrates the problem:

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.
    }
};

Attempting to serialize either the ints object or the Victor object results in error C2338: cereal could not find any output serialization functions for the provided type and archive combination.

Here's the code I use in the main function:

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

What am I doing wrong?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
greentea101
  • 51
  • 1
  • 6
  • 5
    Most probably, you didn't read the [stl support](http://uscilab.github.io/cereal/stl_support.html) part of the documentation and forgot to include relevant header files such as `#include `. As your example is incomplete it's [hard to tell](https://stackoverflow.com/help/mcve) but that would be my guess. – spectras Oct 27 '17 at 01:52
  • Spectras, you're right, thank you. I only read the "Quick Start" page and a couple other pages linked on the main page of the docs. I don't have time right now to test if the code works now, but I don't anticipate any further issues. Thanks again! – greentea101 Oct 27 '17 at 02:09

1 Answers1

0
#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;
}

This code should emit the following output:

{
    "value0": {
        "x": 0,
        "y": 0,
        "z": 0,
        "ints": [
            101,
            202,
            303
        ]
    }
}