我正在Ubuntu 16.04.3。上编码c ++ 11。我已经使用以下命令安装了boost 1.58:apt install libboost-all-dev
。
现在我正在编写有关boost序列化的代码,这是代码:
#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;
}
问题来了:
//iTextArchive >> t2;
iTextArchive >> v;
如果我将iTextArchive
反序列化为t2
,则不会有任何问题。但是,如果我将其反序列化为v
,则该代码仍可以使用命令g++ -std=c++11 test.cpp -lboost_serialization
进行编译,但是当我执行它时,出现了错误:
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): unsupported version
Aborted (core dumped)
我已经在Google上找到了某些内容,例如'boost::archive::archive_exception' what(): unsupported version,但是它不起作用。