错误: 在抛出'boost :: archive :: archive_exception'的实例后终止调用 what():输入流错误 中止(核心倾销)
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
基本上它在序列化时没有问题,但是当反序列化时会引发错误。
由于
答案 0 :(得分:0)
尝试
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;
答案 1 :(得分:0)
当你写下来时,答案就在你的问题中:
oa << Monopoly;
// archive and stream closed when destructors are called <<<<<<<<<<<<<<<<<<<
当您尝试打开文件进行阅读时,文件未关闭。
在阅读测试之前添加ofs.close()
。或者更好的是,将测试的每个部分放在一个{}块中,并在块中声明相关的流变量,这样在离开块范围时归档和流就会被销毁。
答案 2 :(得分:0)
我有这个确切的问题。当您尝试反序列化具有未初始化成员变量的先前序列化类时,Boost序列化会出现此错误。对于您的问题,您将必须将类中的每个变量都初始化为某个合理的值。
换句话说
\0
应该是
...
int cost;
int mortagecost;
int house1cost;
int house2cost;
...
或者您可以初始化类每个实例的每个变量。我希望这会有所帮助。
PS:您没有初始化STL容器。