gpt4 book ai didi

c++ - 序列化包含指针 vector 的结构,每个指针包含其他指针

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:23 26 4
gpt4 key购买 nike

我正在尝试使用 Boost 序列化包含一个 vector 和一个整数的 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 的 vector ,每个 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 对象时抛出异常。

最佳答案

我认为所需要的只是以正确的顺序正确刷新和关闭存档和流。此代码工作正常:

Live On Coliru

#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;
}

打印 Live On Coliru

terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
bash: line 7: 16838 Aborted (core dumped) ./a.out

关于c++ - 序列化包含指针 vector 的结构,每个指针包含其他指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com