[英]Serializing struct containing vector of pointers with each pointer containing other pointers
我正在尝试使用Boost将包含向量和整数的minHeap
对象序列化。
struct minHeap {
std::vector<keyNode> heapStructure; // A vector of many keyNodes
int size; // Current size of vector
minHeap() {
size = 0;
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned version)
{
ar & heapStructure;
ar & size;
}
};
现在heapStructure
是向量keyNode
并且每个keyNode
包含一个字符,整数和对象的两个指针keyNode
本身。
struct keyNode {
char data; // The character itself
int frequency; // Occurances of the character in the data stream.
keyNode * leftNode, *rightNode; // Left and right children of the root node (the keyNode itself)
keyNode() {
data = NULL;
frequency = 0;
leftNode = NULL;
rightNode = NULL;
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned version)
{
ar &data;
ar &frequency;
ar &leftNode;
ar &rightNode;
}
};
以下(示例)代码显示了如何对文件进行序列化和反序列化。
// Write Encoded Stream to File
ofstream outFile;
string outPath = filePath + ".enc";
outFile.open(outPath, ios::out | ios::binary); // TODO: Fix the output path
bitsetR bitStorage(outputStream);
boost::archive::binary_oarchive output(outFile);
output << bitStorage;
output << hTree;
outFile.close();
ifstream inFile;
inFile.open("demo.txt.enc"); // TODO: Fix the output path
bitsetR bitStr("");
boost::archive::binary_iarchive input(inFile);
minHeap temp;
input >> bitStr;
input >> temp;
序列化时没有任何错误,但是反序列化失败并出现以下错误(VS 2017):
Exception Thrown: Input Stream Error (boost::archive::archive_exception)
我应该在这里注意bitsetR
对象成功反序列化。 反序列化minHeap
对象时,将引发异常。
我认为所需要做的只是正确地刷新和关闭档案,并以正确的顺序进行流式处理。 该代码可以正常工作:
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
struct keyNode {
char data = 0; // The character itself
int frequency = 0; // Occurances of the character in the data stream.
keyNode *leftNode = nullptr;
keyNode *rightNode = nullptr; // Left and right children of the root node (the keyNode itself)
private:
friend class boost::serialization::access;
template <class Archive> void serialize(Archive &ar, unsigned) {
ar &data;
ar &frequency;
ar &leftNode;
ar &rightNode;
}
};
struct minHeap {
std::vector<keyNode> heapStructure; // A vector of many keyNodes
size_t size() const { return heapStructure.size(); }
private:
friend class boost::serialization::access;
template <class Archive> void serialize(Archive &ar, unsigned) {
ar &heapStructure;
}
};
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <fstream>
int main() {
minHeap hTree;
{
std::ofstream outFile("demo.txt.enc", std::ios::binary);
{
boost::archive::binary_oarchive output(outFile);
output << hTree;
}
}
{
std::ifstream inFile("demo.txt.enc");
boost::archive::binary_iarchive input(inFile);
minHeap temp;
input >> temp;
}
}
注意我可以通过删除范围使其失败,并显示类似的消息:
int main() {
minHeap hTree;
std::ofstream outFile("demo.txt.enc", std::ios::binary);
boost::archive::binary_oarchive output(outFile);
output << hTree;
std::ifstream inFile("demo.txt.enc");
boost::archive::binary_iarchive input(inFile);
minHeap temp;
input >> temp;
}
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
bash: line 7: 16838 Aborted (core dumped) ./a.out
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.