c++ - std::vector<float> 成员的 boost 序列化/反序列化失败

标签 c++ serialization boost

下午好

我第一次尝试使用 boost::serialization,目前我无法完成一些非常基本的任务。我将我的代码简化为最基本的,首先是一个只有一个 std::vector 成员的简单类的类定义:

#ifndef DATA_H
#define DATA_H

#include <string>
#include <vector>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

namespace ml {
    class Data {
        private:
            std::vector<float> d;

            friend class boost::serialization::access;

            template<class Archive>
            void serialize(Archive& ar, const unsigned int version);
        public:

            static Data Load(const std::string& filename);

            Data(const std::vector<float>& d);

            Data(const Data& rhs);
            Data();

            ~Data();

            Data& operator=(const Data& rhs);

            void save(const std::string& filename);

            std::vector<float> getD() const;
    };
}
#endif // DATA_H 

实现:

#include <iostream>
#include <fstream>

#include <boost/serialization/vector.hpp>

#include "Data.h"

ml::Data::Data(const std::vector<float>& d) : d(d)
{
}

ml::Data::Data(const Data& rhs)
{
    d = rhs.d;
}

ml::Data::Data()
{

}

ml::Data& ml::Data::operator=(const Data& rhs)
{
    d = rhs.d;

    return *this;
}

ml::Data ml::Data::Load(const std::string& filename)
{
    Data data;

    std::ifstream file(filename.c_str());

    boost::archive::text_iarchive ia(file);

    ia >> data;

    return data;
}

ml::Data::~Data()
{
}

void ml::Data::save(const std::string& filename)
{
    std::ofstream file(filename.c_str());    

    boost::archive::text_oarchive oa(file);

    oa << this;
}

std::vector<float> ml::Data::getD() const
{
   return d;
}

template<class Archive>
void ml::Data::serialize(Archive& ar, const unsigned int version)
{
   ar & d;

   std::cout << "Debug: " << d.size() << std::endl;
}

还有一个非常小的主程序:

#include <iostream>

#include "Data.h"

int main(int argc, char** argv)
{
    const std::string filename("./test.txt");
    const int limit = 100;

    std::vector<float> orig;
    std::vector<float> copy;

    for(int i=0; i<limit; ++i) {
        orig.push_back(i);
    }

    ml::Data origData(orig);
    origData.save(filename);

    ml::Data copyData = ml::Data::Load(filename);

    orig = origData.getD();
    copy = copyData.getD();

    std::cout << "Sizes: "    << std::endl;
    std::cout << "Original: " << orig.size() << std::endl;
    std::cout << "Copy: "     << copy.size() << std::endl;

    return 0;
}

程序输出为:

Debug: 100 
Debug: 0 
Sizes:  
Original: 100 
Copy: 0

序列化和文件输出似乎有效,生成的文件看起来符合预期,但反序列化似乎失败了。没有进一步的错误输出或任何提示,哪里出了问题。如果感兴趣,Boost 版本是 1.60.0。

那么,知道出了什么问题吗?

提前谢谢你。

最佳答案

您的序列化序列化了一个指针:

oa << this;

反序列化反序列化为引用:

Data data;
ia >> data;

这是一个错误。结果未定义。修复它:

oa << *this;

演示

Live On Coliru

打印:

Debug (loading: false) 5
Original: 5
Debug (loading: true) 5
Copy: 5

代码:

#ifndef DATA_H
#define DATA_H

#include <string>
#include <vector>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

namespace ml {
    class Data {
        private:
            std::vector<float> d;

            friend class boost::serialization::access;

            template<class Archive>
            void serialize(Archive& ar, const unsigned int version);
        public:

            static Data Load(const std::string& filename);

            Data(const std::vector<float>& d) : d(d) {}
            //Data(const Data& rhs) = default;
            Data() = default;
            //~Data() = default;
            //Data& operator=(const Data& rhs) = default;

            void save(const std::string& filename);

            std::vector<float> getD() const;
    };
}
#endif // DATA_H 

#include <iostream>
#include <fstream>

#include <boost/serialization/vector.hpp>

//#include "Data.h"
ml::Data ml::Data::Load(const std::string& filename)
{
    Data data;

    std::ifstream file(filename.c_str());

    boost::archive::text_iarchive ia(file);

    ia >> data;

    return data;
}

void ml::Data::save(const std::string& filename)
{
    std::ofstream file(filename.c_str());    

    boost::archive::text_oarchive oa(file);

    oa << *this;
}

std::vector<float> ml::Data::getD() const
{
   return d;
}

template<class Archive>
void ml::Data::serialize(Archive& ar, const unsigned int /*version*/)
{
   ar & d;

   std::cout << "Debug (loading: " << std::boolalpha << typename Archive::is_loading::type() << ") " << d.size() << std::endl;
}

#include <iostream>

int main()
{
    const std::string filename("./test.txt");
    const int limit = 5;

    {
        std::vector<float> orig;

        for(int i=0; i<limit; ++i) { orig.push_back(i); }

        ml::Data origData(orig);
        origData.save(filename);

        orig = origData.getD();
        std::cout << "Original: " << orig.size() << std::endl;
    }

    ml::Data copyData = ml::Data::Load(filename);
    std::cout << "Copy: " << copyData.getD().size() << std::endl;
}

关于c++ - std::vector<float> 成员的 boost 序列化/反序列化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43454745/

相关文章:

c++ - 序列化多态类型的常见混淆

c++ - boost asio async_read header 连接过早关闭

c++ - 加载共享库时出错 : libboost_system. 所以 1.49.0: 没有这样的文件或目录

c++ - 使用链表C++数组的优先级队列

c++ - 使用不在 PATH 中的 minGW 和 eclipse CDT

c# - .net DateTime 序列化反序列化 bug

json - 如何设置 JsonRequestBehavior.AllowGet 并返回 JsonResult

c++ - 需要帮助使用 Boost Property Tree 库和 XML 递归创建目录树

c++ - 避免 RAII 计时器对象中的虚假构造和破坏

c++ - 如何实现 C++ vector 类以允许动态调整数组大小?