0

我正在为一个类做一个最终项目,并试图使用谷物库来序列化一个结构,该结构包含另一个类型结构的向量向量,但这是失败的。另一种(包含在向量中)类型的结构具有有效的序列化功能(经过测试)。当我尝试序列化和反序列化容器结构时,我收到错误“无法从输入流中读取 8 个字节!读取 0”

我已经成功地序列化了另一个包含结构向量向量的结构,所以我知道这应该是可能的。当我这样做时,向量中结构中的所有字符串都可以在我保存输出的文件中看到。但是,当我为这个项目序列化容器结构时,生成的文件只包含容器类中向量的第一个子向量中的前两个内部结构的字符串。(例如:包含 struct.vec[0][0].string 和 struct.vec[0][1].string,但不包含其他 14 个)。

在包含容器结构的文件的顶部,我有

#include <vector>
#include <string>
#include <random>
#include "region.h"
#include "constants.h"
#include "cereal/archives/binary.hpp"
#include "cereal/types/vector.hpp"
#include "cereal/types/string.hpp"

它具有以下成员变量(此处重命名)。Region 是一个具有工作序列化功能的结构。:

vector<vector<Region>> game_map;
int x;
int y;

容器的(Country)序列化函数:

template<class Archive>
void serialize(Archive &archive) {
    archive(game_map, x, y);
}

这是产生错误的代码:

TEST_CASE("Serialize manually created country", "[serialize]") {
        Here, I create a vector of vectors of Regions called new_game_map, and I think I did so correctly, as I used it to create country later and that had the right values for everything (checked with printing).
    { //First creating country object, serializing it, and saving it to a file
        stringstream ss;
        cereal::BinaryOutputArchive oarchive(ss);

        Country country;
        country.game_map = new_game_map;
        country.x = 49;
        country.y = -3;

        oarchive(country);

        ofstream output_stream;
        output_stream.open("test_cereal_save_file.txt");
        string save;
        ss >> save;
        output_stream << save;
        output_stream.close();
    }

    { //Now getting saved data and turning it back into a country object, then checking that it equals original.
        stringstream ss;
        string save;
        ifstream input_stream;
        input_stream.open("test_cereal_save_file.txt");
        input_stream >> save;
        ss << save;
        input_stream.close();
        cereal::BinaryInputArchive iarchive(ss);

        Country country;
        iarchive(country);

        Country expected_country;
        expected_country.game_map = new_game_map;
        expected_country.x = 49;
        expected_country.y = -3;

        REQUIRE(expected_country == country);
    }

我希望用谷物创造的国家与我后来创造的国家相同,但事实并非如此。它不包含任何变量,并且 Catch2 提供了错误“无法从输入流中读取 4 个字节!读取 0”

4

0 回答 0