首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ boost反序列化引发错误

C++ boost反序列化引发错误
EN

Stack Overflow用户
提问于 2014-11-15 22:17:06
回答 3查看 182关注 0票数 1

错误:在引发'boost::archive::archive_exception‘what()的实例后调用terminate :输入流错误已中止(核心转储)

Message.h:

代码语言:javascript
复制
/*
    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

代码语言:javascript
复制
// 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

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

谢谢

EN

回答 3

Stack Overflow用户

发布于 2014-11-15 23:06:40

试一试

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-11-15 23:06:43

答案就在你的问题中,当你写道:

代码语言:javascript
复制
   oa << Monopoly;
    // archive and stream closed when destructors are called <<<<<<<<<<<<<<<<<<<

当您尝试打开要读取的文件时,该文件未关闭。

在阅读测试之前添加一个ofs.close()。或者更好的做法是,将测试的每个部分放在一个{}块中,并在块中声明相关的流变量,这样当离开块作用域时,存档和流就会被销毁。

票数 0
EN

Stack Overflow用户

发布于 2018-06-27 08:42:31

我就遇到了这个问题。当您尝试反序列化具有未初始化成员变量的先前序列化的类时,Boost序列化会导致此错误。对于您的问题,您必须将类中的每个变量初始化为某个合理的值。

换句话说,

代码语言:javascript
复制
...
int cost;
int mortagecost;
int house1cost;
int house2cost;
...

应该是

代码语言:javascript
复制
...
int cost = 0;
int mortagecost = 0;
int house1cost = 0;
int house2cost = 0;
...

或者您可以初始化类的每个实例化的每个变量。我希望这能帮到你。

PS:你没有初始化STL容器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26946748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档