gpt4 book ai didi

C++ boost反序列化抛出错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:05 24 4
gpt4 key购买 nike

错误:在抛出“boost::archive::archive_exception”实例后调用终止 what(): 输入流错误中止(核心转储)

消息.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

基本上它在序列化时没有问题,但在反序列化时会抛出该错误。

谢谢

最佳答案

尝试

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;

关于C++ boost反序列化抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26946748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com