I am working on serialization of color data for SFML objects and ran into an issue where they are not supported by default, as they are not a default type. I tried making a wraparound class, which failed but I found info of adding the type to cereal itself. Based off of what I read at Serialize/deserialize SFML Vectors class using cereal, the following code supposedly should work, I have this currently in a .h file.
#include <SFML/Graphics.hpp>
#include <SFML/Config.hpp>
namespace /*cereal*/ sf
{
template<class Archive>
void serialize(Archive& archive, sf::Color& c)
{
archive(
CEREAL_NVP(this->r),
CEREAL_NVP(this->g),
CEREAL_NVP(this->b),
CEREAL_NVP(this->a)
);
}
}
however, I end up with an error still, as follows:
Error C2338 cereal could not find any output serialization functions for the provided type and archive combination. SFML_Tiles C:\Users\97cwe\Documents\Visual Studio Libraries\Cereal\cereal-1.3.2\include\cereal\cereal.hpp 570
I cannot parse the documentation to understand it more, nor does the stack overflow post linked above offer any insight. I probably need to include it somewhere, but as I don't know where, or even if this is the right format, I am asking here
Any help will be greatly appreciated. Thank you
dergvern47 on Reddit helped me with this. The issue was 2 fold. Accessing the values inside cannot be done with this, but with the object inside the function, and that the .h file this is inside needs to be #include in the highest .h file existing in the project. The original post can be found here https://www.reddit.com/r/cpp_questions/comments/ul7c3v/c_cereal_add_serialization_to_class_in_imported/i7tswiv/?context=3
and the code that they made:
#include <SFML/Graphics.hpp>
#include <cereal/archives/json.hpp>
namespace sf {
template<class Archive>
void serialize(Archive& archive, sf::Color& c)
{
archive(
CEREAL_NVP(c.r),
CEREAL_NVP(c.g),
CEREAL_NVP(c.b),
CEREAL_NVP(c.a)
);
}
}
int main()
{
{
cereal::JSONOutputArchive archive(std::cout);
archive(sf::Color::Magenta);
}
return 0;
}