提升序列化无法恢复已保存的对象

时间:2015-05-13 18:44:25

标签: c++ serialization boost boost-serialization

这是我班级的样子:

#include <iostream>
#include <boost/dynamic_bitset/dynamic_bitset.hpp>
#include <vector>

#include <fstream>

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;
using namespace boost;

class outer{
    friend class boost::serialization::access;
    public:
        int a;
        class inner{
            friend class boost::serialization::access;
            public:
                int a;
                inner(int a){
                    this->a = a;
                }
                template<class Archive>
                void serialize(Archive & ar,const unsigned int version){
                    ar & a;
                }
                inner(){
                }
        };
        vector<inner> inners;
        outer(int a){
            this->a = a;    
            inners.push_back(inner(this->a+1));     
        }
        outer(){
        }

        template<class Archive>
        void serialize(Archive & ar,const unsigned int version){
            ar & a;
            ar & inners;
        }
};

vector<outer> outers;
vector<outer> _outers;

BOOST_CLASS_EXPORT(outer);

int main(int, char*[]) {
    int i;
    std::ofstream ofs("filename.dat");
    for(i=0;i<5;i++)
        outers.push_back(outer(i)); 

    boost::archive::text_oarchive oa(ofs);
    oa << outers;
    std::ifstream ifs("filename.dat");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> _outers;
  return EXIT_SUCCESS;
}

我创建了一个outer类实例的向量,并将其写入文本流(似乎工作正常)。但是当我读回来时,我得到了错误:
在抛出'boost :: archive :: archive_exception'的实例后终止调用   what():输入流错误

以上是完整的自包含代码,编译时使用:

g++ -I /path/to/boost test.cpp -lboost_serialization

我如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

失踪:

#include <boost/serialization/export.hpp>

您需要在读取文件之前关闭输出存档/流:

int main() {
    {
        std::ofstream ofs("filename.dat");

        std::vector<outer> outers(5);
        std::iota(outers.begin(), outers.end(), 0u);

        {
            boost::archive::text_oarchive oa(ofs);
            oa << outers;
        }
    }

    {
        // read class state from archive
        std::vector<outer> _outers;
        std::ifstream ifs("filename.dat");
        boost::archive::text_iarchive ia(ifs);
        ia >> _outers;

        for(auto& outer : _outers) {
            std::cout << "outer " << outer.a << "\n";

            for (auto& inner: outer.inners)
                std::cout << "   inner " << inner.a << "\n";
        }
    }
}

完整演示

<强> Live On Coliru

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <fstream>

class outer {
    friend class boost::serialization::access;

  public:
    int a;
    class inner {
        friend class boost::serialization::access;

      public:
        int a;
        inner(int a) { this->a = a; }
        template <class Archive> void serialize(Archive &ar, unsigned) { ar &a; }
        inner() {}
    };
    std::vector<inner> inners;
    outer(int a = 0) : a(a), inners {a+1} { }

    template <class Archive> void serialize(Archive &ar, unsigned) {
        ar &a;
        ar &inners;
    }
};


BOOST_CLASS_EXPORT(outer)

int main(int, char *[]) {
    {
        std::ofstream ofs("filename.dat");

        std::vector<outer> outers(5);
        std::iota(outers.begin(), outers.end(), 0u);

        {
            boost::archive::text_oarchive oa(ofs);
            oa << outers;
        }
    }

    {
        // read class state from archive
        std::vector<outer> _outers;
        std::ifstream ifs("filename.dat");
        boost::archive::text_iarchive ia(ifs);
        ia >> _outers;

        for(auto& outer : _outers) {
            std::cout << "outer " << outer.a << "\n";

            for (auto& inner: outer.inners)
                std::cout << "   inner " << inner.a << "\n";
        }
    }
}

打印

outer 0
   inner 1
outer 1
   inner 2
outer 2
   inner 3
outer 3
   inner 4
outer 4
   inner 5