cereal
cereal copied to clipboard
error: ‘int MyCoolClass::secretX’ is private within this context
Hello, I'm trying to create some complex object which would be cached from disk if the previous run of the program was successful. The basic idea is implemented in the two enclosed file.
The idea: check if some file exists in the ctor. If yes, load the object from it; otherwise construct and dump the object. This is achieved by the call "archive (*this)"
Compilation with c++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 as c++ -g -c -o toto.o toto.cpp -std=c++11 results in toto.cpp:15:28: error: ‘int MyCoolClass::secretX’ is private within this context archive(m.x, m.y, m.z, m.secretX, m.enclosed);
The sources and full error messages are enclosed in a zip file. Any hint ?
Regards
Pascal
Code for other's reference:
#include <time.h>
#include <cereal/access.hpp>
class SimpleClass
{
public:
explicit SimpleClass(struct tm const &);
private:
struct tm date;
SimpleClass();
friend class MyCoolClass;
friend class cereal::access;
};
class MyCoolClass
{
public:
MyCoolClass(int x, int y, int z): x(x), y(y), z(z), secretX(-1) {};
int x, y, z;
private:
MyCoolClass();
int secretX;
SimpleClass enclosed;
friend class cereal::access;
};
#include <cereal/archives/json.hpp>
#include "toto.hpp"
MyCoolClass::MyCoolClass(): secretX(0), enclosed()
{
cereal::JSONOutputArchive archive(std::cout);
archive(*this);
};
template<class Archive>
void save(Archive & archive,
MyCoolClass const & m)
{
archive(m.x, m.y, m.z, m.secretX, m.enclosed);
};
template<class Archive>
void load(Archive & archive,
MyCoolClass & m)
{
archive(m.x, m.y, m.z, m.secretX, m.enclosed);
};
template<class Archive>
void save(Archive & archive,
SimpleClass const & m)
{
archive(m.date);
};
template<class Archive>
void load(Archive & archive,
SimpleClass & m)
{
archive(m.date);
};
You've given a specific class within cereal (cereal::access) access to your member variables with the friend class cereal:;access, but you try to access the member variables directly in the non-member save function, which does not have access to the data. If you wanted this function to access those private members, you would need to have it explicitly listed as a friend as well. Check out the documentation of friend functions for more information.