Reputation: 144
I try to serialize and deserialize an object with boost::serialize library. I need to split my save and load function.
I use the library is described in the official tutorial. My save and load functions look like this:
friend class boost::serialization::access;
template<typename Archive>
void save(Archive& ar, const unsigned version) const {
ar & name;
ar & NType;
ar & NTherm;
ar & NRun;
//...
}
template<class Archive>
void load(Archive& ar, const unsigned int version) {
ar & name;
ar & NType;
ar & NTherm;
ar & NRun;
//...
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
These functions are implemented in the header of a class. I serialize and deserialize like this: { //Serialize std::ofstream ofs("output.txt"); boost::archive::text_oarchive oa(ofs); oa << Object; }
{
//Deserialize
Class newObject;
std::ifstream ifs("output.txt");
boost::archive::text_iarchive ia(ifs);
ia >> newObject;
}
Serialization works fine but while deserialization throws an exception at ar & NRun;
.
An error message pops up which says: This application has requested the Runtime to terminate it in an unusual way. Debugging showed that an exception class name too long was thrown.
How can I fix this?
Update: Added brackets in the code snippet.
Update2: I added an SSCCE.
main.cpp:
#include <iostream>
#include "simulation.h"
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/split_member.hpp>
int main()
{
Simulation *sim;
sim = new Simulation(2,25,25,25,100,500,1000,"Sim");
{
std::ofstream ofs("output.txt");
boost::archive::text_oarchive oa(ofs);
oa << sim;
}
{
Simulation newSim;
std::ifstream ifs("output.txt",std::ios::binary);
boost::archive::text_iarchive ia(ifs);
ia >> newSim;
}
}
simulation.h:
#ifndef SIMULATION_H_
#define SIMULATION_H_
#include <string>
#include <boost/serialization/access.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/string.hpp>
class Simulation {
public:
//Constructors
Simulation(int anzType, int x=25, int y=25, int z=25, int NT = 100, int NS = 500, int NR = 1000, std::string n = "");
Simulation(); //Defaultconstructor für Boost Serialisierung
//Destructor
virtual ~Simulation();
private:
int NType;
int NTherm;
int NStep;
int NRun;
std::string name;
int Lx;
int Ly;
int Lz;
int LyLz;
friend class boost::serialization::access;
template<typename Archive>
void save(Archive& ar, const unsigned version) const {
ar & name;
ar & NType;
ar & NTherm;
ar & NRun;
ar & NStep;
}
template<class Archive>
void load(Archive& ar, const unsigned int version) {
ar & name;
ar & NType;
ar & NTherm;
ar & NRun;
ar & NStep;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
#endif /* SIMULATION_H_ */
simulation.cpp:
#include "Simulation.h"
Simulation::Simulation() {
}
Simulation::Simulation(int anzType, int x, int y, int z, int NT, int NS, int NR, std::string n) {
name = n;
NType = anzType;
NTherm = NT;
NStep = NS;
NRun = NR;
Lx = x;
Ly = y;
Lz = z;
LyLz = y*z;
}
Simulation::~Simulation() {
}
Upvotes: 1
Views: 318
Reputation: 392999
UPDATE Since you updated the question with a SSCCE, it's obvious.
You serialize a Simulation*
. And then you try to deserialize a Simulation&
. Unsurprisingly, this won't work.
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <fstream>
#include <iostream>
class Simulation {
public:
// Constructors
Simulation(int anzType, int x = 25, int y = 25, int z = 25, int NT = 100, int NS = 500, int NR = 1000, std::string n = "");
Simulation(); // Defaultconstructor für Boost Serialisierung
// Destructor
virtual ~Simulation();
private:
std::string name;
int NType, NTherm, NStep, NRun;
int Lx, Ly, Lz, LyLz;
friend class boost::serialization::access;
template <typename Archive> void serialize(Archive &ar, unsigned) {
ar & name;
ar & NType;
ar & NTherm;
ar & NRun;
ar & NStep;
}
};
Simulation::Simulation() {}
Simulation::Simulation(int anzType, int x, int y, int z, int NT, int NS, int NR, std::string n)
: name(n), NType(anzType), NTherm(NT), NStep(NS), NRun(NR),
Lx(x), Ly(y), Lz(z), LyLz(y * z)
{
}
Simulation::~Simulation() {}
int main() {
Simulation *sim = new Simulation(2, 25, 25, 25, 100, 500, 1000, "Sim");
{
std::ofstream ofs("output.txt", std::ios::binary);
boost::archive::text_oarchive oa(ofs);
oa << sim;
}
{
Simulation* newSim = nullptr;
std::ifstream ifs("output.txt", std::ios::binary);
boost::archive::text_iarchive ia(ifs);
ia >> newSim;
delete newSim;
}
}
Upvotes: 2