序列化包含指针向量的struct,每个指针包含其他指针

时间:2017-12-16 15:58:50

标签: c++ pointers serialization boost deserialization

我正在尝试使用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;
    }
}; 

现在heapStructurekeyNode的向量,每个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对象时抛出异常。

1 个答案:

答案 0 :(得分:0)

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

<强> 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