使用映射增强二进制序列化,并在序列化时双打崩溃

发布于 2024-11-10 14:53:38 字数 2444 浏览 2 评论 0原文

这是代表我的问题的示例。除非 FinalTime 大于 25,否则映射将完美地序列化。通过 boost 单元测试,我得到了 std::exception 输入流错误。另外,此代码使用 polymorphic_text_archives 可以正常工作。读取地图时发生错误。

#include <fstream>
#include <iostream>
#define BOOST_ALL_DYN_LINK
#define BOOST_PARAMETER_MAX_ARITY 8
#include <boost/serialization/export.hpp>
#include <boost/archive/polymorphic_iarchive.hpp>
#include <boost/archive/polymorphic_oarchive.hpp>
#include <boost/archive/polymorphic_binary_iarchive.hpp>
#include <boost/archive/polymorphic_binary_oarchive.hpp>
#include <boost/static_assert.hpp>
#include <boost/serialization/map.hpp>

using namespace boost;

class DoubleTest
{
public:
    DoubleTest(double d)
    {
        this->Double = d;
    }

    DoubleTest()
    {
        this->Double = 0;
    }

    void serialize(boost::archive::polymorphic_iarchive & ar, const unsigned int)
    {
        ar & this->Double;
    }

    void serialize(boost::archive::polymorphic_oarchive & ar, const unsigned int)
    {
        ar & this->Double;
    }

    double Double;
};

int main(int argc, char** argv)
{
    const std::string fileName = "test.out";
    std::map<float, DoubleTest*> mymap;

    const int initialValue = 0;
    const int initialTime = 0;

    // A "finalTime" of 25 works. "26" does not.
    const int finalTime = 26;
    int value = 10;

    // Put values into the map.
    for(int time = initialTime; time < finalTime; time += 1)
    {
        value++;
        mymap[time] = new DoubleTest(value);
    }

    // Write a binary archive out.
    std::ofstream ofs(fileName.c_str());
    boost::archive::polymorphic_binary_oarchive oa(ofs);
    oa << mymap;
    ofs.flush();
    ofs.close();

    // Create a new map to read the binary archive into.
    std::map<float, DoubleTest*> mymap2;

    // Read in the new binary archive.
    std::ifstream ifs(fileName.c_str());
    boost::archive::polymorphic_binary_iarchive ia(ifs);
    ia >> mymap2;
    ifs.close();

    // Loop through the values to make sure they are correct.
    std::map<float, DoubleTest*>::iterator it; 
    for(it = mymap2.begin(); it != mymap2.end(); ++it)
    {
        std::cout << "Key: " << it->first << " ";
        std::cout << "Value: " << it->second->Double << '\n';
    }

    int pause;
    std::cin >> pause;
}

This is a sample that represents my problem. The map will serialize perfectly fine unless finalTime is greater than 25. With boost unit testing I was given a std::exception input stream error. Also, this code works fine using polymorphic_text_archives. The error occurs while reading in the map.

#include <fstream>
#include <iostream>
#define BOOST_ALL_DYN_LINK
#define BOOST_PARAMETER_MAX_ARITY 8
#include <boost/serialization/export.hpp>
#include <boost/archive/polymorphic_iarchive.hpp>
#include <boost/archive/polymorphic_oarchive.hpp>
#include <boost/archive/polymorphic_binary_iarchive.hpp>
#include <boost/archive/polymorphic_binary_oarchive.hpp>
#include <boost/static_assert.hpp>
#include <boost/serialization/map.hpp>

using namespace boost;

class DoubleTest
{
public:
    DoubleTest(double d)
    {
        this->Double = d;
    }

    DoubleTest()
    {
        this->Double = 0;
    }

    void serialize(boost::archive::polymorphic_iarchive & ar, const unsigned int)
    {
        ar & this->Double;
    }

    void serialize(boost::archive::polymorphic_oarchive & ar, const unsigned int)
    {
        ar & this->Double;
    }

    double Double;
};

int main(int argc, char** argv)
{
    const std::string fileName = "test.out";
    std::map<float, DoubleTest*> mymap;

    const int initialValue = 0;
    const int initialTime = 0;

    // A "finalTime" of 25 works. "26" does not.
    const int finalTime = 26;
    int value = 10;

    // Put values into the map.
    for(int time = initialTime; time < finalTime; time += 1)
    {
        value++;
        mymap[time] = new DoubleTest(value);
    }

    // Write a binary archive out.
    std::ofstream ofs(fileName.c_str());
    boost::archive::polymorphic_binary_oarchive oa(ofs);
    oa << mymap;
    ofs.flush();
    ofs.close();

    // Create a new map to read the binary archive into.
    std::map<float, DoubleTest*> mymap2;

    // Read in the new binary archive.
    std::ifstream ifs(fileName.c_str());
    boost::archive::polymorphic_binary_iarchive ia(ifs);
    ia >> mymap2;
    ifs.close();

    // Loop through the values to make sure they are correct.
    std::map<float, DoubleTest*>::iterator it; 
    for(it = mymap2.begin(); it != mymap2.end(); ++it)
    {
        std::cout << "Key: " << it->first << " ";
        std::cout << "Value: " << it->second->Double << '\n';
    }

    int pause;
    std::cin >> pause;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-11-17 14:53:39

您需要序列化为二进制文件流。将 ios_base::binary 添加到流构造函数中。

You need to serialize to a binary file stream. Add the ios_base::binary to the stream constructors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文