I want to serialize classes that have std::shared_ptr s of each other as members and that are declared and defined in different files. A minimal example of my code would be:
//auxiliary.h
#include <memory>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
class A;
class B;
typedef std::shared_ptr< A > A_ptr;
typedef std::shared_ptr< B > B_ptr;
//A.h
#include "auxiliary.h"
class A{
B_ptr bpt;
void foo();
//...
};
//A.cpp
#include "A.h"
void A::foo()
{
//implementation
}
//B.h
#include "auxiliary.h"
class B{
A_ptr apt;
void bar();
//...
};
//B.cpp
#include "B.h"
void B::bar()
{
//implementation
}
when I try to serialize both classes now by writing
//A.h
#include "auxiliary.h"
class A{
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & B_ptr;
}
B_ptr bpt;
void foo();
//...
};
and
//B.h
#include "auxiliary.h"
class B{
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & A_ptr;
}
A_ptr apt;
void bar();
//...
};
I get the error C2139 "A": an undefined class is not allowed as an argument to compiler intrinsic type __is_base_of
I understand that the compiler would rather see the serialization functions defined in the corresponding cpp files, but that won't work in the usual way as they are templates.
How could I possibly fix the whole thing?
PS.: I also read, that it helped in similar situation to instanciate the template with all the variants one uses and boost states in the manual, that this should be done somewhere via
template void serialize<boost::archive::text_iarchive>(
boost::archive::text_iarchive & ar,
const unsigned int file_version
);
template void serialize<boost::archive::text_oarchive>(
boost::archive::text_oarchive & ar,
const unsigned int file_version
I've tried various ways to do it, but with no success.