下午好,
我第一次玩boost :: serialization,目前我正在完成一些非常基本的任务。我把我的代码剥离到最基本的,首先是类定义,只有一个std :: vector成员的简单类:
#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
程序输出为:
{{1}}
序列化和文件输出似乎工作,生成的文件看起来像预期的那样,但反序列化似乎失败了。没有进一步的错误输出或任何提示,出了什么问题。如果感兴趣的话,Boost版本是1.60.0。
那么,知道出了什么问题吗?
提前谢谢。
答案 0 :(得分:2)
序列化序列化指针:
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;
}