Boost序列化无法恢复保存的对象

Boost serialization unable to restore saved objects

这是我的 class 的样子:

#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 class 个实例的向量,并将其写入文本流(这似乎工作正常)。但是当我读回来时,我得到了错误:
在抛出 'boost::archive::archive_exception' 的实例后调用终止 what(): 输入流错误

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

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

我该如何解决这个问题,有什么想法吗?

失踪:

#include <boost/serialization/export.hpp>

您需要在读回文件之前关闭输出 archive/stream:

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