dimanche 21 avril 2019

boost::serialization with variant: unsupported version

I'm coding c++11 on Ubuntu 16.04.3. I've installed the boost 1.58 with the command: apt install libboost-all-dev.

Now I'm coding about the boost serialization, here is the code:

#include <iostream>
#include <fstream>
#include <boost/serialization/variant.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>


class Test {
public:
    Test() = default;
    template <typename Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & a & b & ch;
    }

    int a, b;
    char ch;
};

class Test2
{
public:
    Test2() = default;
    template <typename Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & a & b;
    }

    double a, b;
};

int main()
{
    Test t;
    const char *filename = "ser";
    // serialize
    std::ofstream ofile(filename);
    boost::archive::text_oarchive oTextArchive(ofile);
    oTextArchive << t;
    ofile.close();
    // deserialize
    std::ifstream ifile(filename);
    boost::archive::text_iarchive iTextArchive(ifile);
    Test t2;
    boost::variant<Test, Test2> v;
    //iTextArchive >> t2;
    iTextArchive >> v;

    ifile.close();

    return 0;
}

The problem comes here:

//iTextArchive >> t2;
iTextArchive >> v;

If I deserialize iTextArchive to t2, nothing will be wrong. But if I deserialize it to v, the code is still compilable with the command g++ -std=c++11 test.cpp -lboost_serialization , but when I executed it, I got an error:

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  unsupported version
Aborted (core dumped)

I've found something on Google, such as 'boost::archive::archive_exception' what(): unsupported version, but nothing could help me on this problem for now.

Aucun commentaire:

Enregistrer un commentaire