Reputation: 19
Error : terminate called after throwing an instance of 'boost::archive::archive_exception' what(): input stream error Aborted (core dumped)
Message.h :
/*
GameInit.cpp
Created by :
Divyanshu Rathi
5/09/2014
*/
#ifndef __GAMEINIT_H_INCLUDED__
#define __GAMEINIT_H_INCLUDED__
#include <iostream>
#include <string>
#include <vector>
#include "math.h"
#include <stdio.h>
#include <float.h>
#include <cstdlib>
#include <ctime>
#include <boost/archive/tmpdir.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;
extern bool isturncompleted[5];
extern int whichplayerturn;
extern int startlocationno;
extern int kplane;
extern float jplane;
extern bool showplanetransition;
extern int playerwhocanbuy;
extern int whichplayeristhis;
class City
{
public:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & *name;
ar & group;
ar & cost;
ar & mortagecost;
ar & house1cost;
ar & house2cost;
ar & house3cost;
ar & house4cost;
ar & rent0;
ar & rent1;
ar & rent2;
ar & rent3;
ar & rent4;
ar & renthotel;
ar & isbought;
ar & boughtby;
ar & totalhouses;
ar & ishotel;
}
string* name;
int group;
int cost;
int mortagecost;
int house1cost;
int house2cost;
int house3cost;
int house4cost;
int hotelcost;
int rent0;
int rent1;
int rent2;
int rent3;
int rent4;
int renthotel;
int isbought;
int boughtby;
int totalhouses;
int ishotel;
};
class Game
{
public:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & startingmoney;
ar & jailfine;
ar & tax;
ar & map;
ar & totalcolorgroups;
ar & colorcodes;
ar & whichplayerturn;
ar & isturncompleted;
ar & hismoney;
cout << "robin"<< endl;
ar & citiesowned;
ar & noofcitiesowned;
ar & citiesofeachgroup;
ar & currentlocation;
ar & temporarylocation;
ar & cities;
ar & *currency;
}
string* currency;
int startingmoney;
int jailfine;
int tax;
vector <City> cities;
int map[50][50];
int totalcolorgroups;
float colorcodes[20][3];
int whichplayerturn=1;
bool isturncompleted[5]={1,1,1,1,1};
void Generatecolorgroups();
//string *name[5];
float hismoney[5];
int citiesowned[5][100][5];
int noofcitiesowned[5];
int citiesofeachgroup[5][50];
int currentlocation[5];
int temporarylocation[5];
};
#endif
Main.cpp
// create and open a character archive for output
std::ofstream ofs("filename");
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << Monopoly;
// archive and stream closed when destructors are called
Game newg;
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
// archive and stream closed when destructors are called
Basically it gives no problem while serializing but when deserialized it throws up that error.
Thanks
Upvotes: 1
Views: 245
Reputation: 754
I had this exact problem. Boost serialization gives this error when you try to deserialize a previously serialized class that has uninitialized member variables. For your problem, you will have to initialize every variable in your class to some sensible value.
In other words,
...
int cost;
int mortagecost;
int house1cost;
int house2cost;
...
should be
...
int cost = 0;
int mortagecost = 0;
int house1cost = 0;
int house2cost = 0;
...
or you can initialize every variable of every instantiation of your class. I hope this helps.
PS: you don't have initialize STL containers.
Upvotes: 0
Reputation: 73376
The answer is in your question, when you write:
oa << Monopoly;
// archive and stream closed when destructors are called <<<<<<<<<<<<<<<<<<<
The file in not closed, when you try to open it for reading.
Add an ofs.close()
before your reading test. Or better, put each part of the test in a { } block with related stream variable declared inside the block, so that archive and stream gets destroyed when leaving the block scope.
Upvotes: 0
Reputation: 60979
Try
std::ofstream ofs("filename");
boost::archive::text_oarchive oa(ofs);
oa << Monopoly;
ofs.close(); // <= HERE
Game newg;
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
ia >> newg;
Upvotes: 1