I am working on a final project for a class and am trying to use the cereal library to serialize a struct that contains a vector of vectors of another type of structs, and this is failing. The other (contained within vector) type of struct has a serialization function that works (tested). When I try to serialize and deserialize the container struct, I get the error "Failed to read 8 bytes from input stream! Read 0"
I have successfully serialized another struct which contains a vector of vectors of structs, so I know that should be possible. When I did that, all strings that were in the struct in the vectors could be seen in the file I saved the output to. However, when I serialized the container struct for this project, the resulting file contains only the strings of the first two internal structs in the first subvector of the vector in the container class. (ex: contains struct.vec[0][0].string and struct.vec[0][1].string, but not any of the 14 other ones).
At the top of the file containing the container struct, I have
#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"
It has the following member variables (renamed here). Region is a struct with a working serialization function.:
vector<vector<Region>> game_map;
int x;
int y;
The container's (Country) serialization function:
template<class Archive>
void serialize(Archive &archive) {
archive(game_map, x, y);
}
This is the code producing the error:
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);
}
I expect the country created with cereal to equal the one I made later, but it does not. It contains no variables and Catch2 provided the error "Failed to read 4 bytes from input stream! Read 0"