2
我正在使用boost序列化來存儲和加載幾個類。我的目標是在一個包含其他類的類上使用序列化,因此其他類將被序列化。使用Boost序列化與前向聲明類和繼承
但問題是這些類包含前向聲明和繼承,我不能使這些類的boost序列化工作。
我在編譯有問題,特別是關於提前聲明,這些錯誤:
error: invalid use of incomplete type ‘class C’
error: forward declaration of ‘class C’
error: ‘value’ is not a member of ‘boost::is_polymorphic<C>’
...
有人能告訴我什麼是錯我的代碼?我錯過了什麼嗎? 我的代碼是用於序列化派生和前向聲明的類是否正確?
阿:
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
class B;
class C;
class A {
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & m_Bvector;
}
protected:
vector<B*> m_Bvector;
vector<C*> m_Cvector;
/*....*/
}
NB:載體m_Bvector可以包含B *或/和C *對象
了Bh:
#include <boost/serialization/access.hpp>
#include "A.h"
class B {
private :
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & m_someInt;
}
protected :
int m_someInt;
/*....*/
}
章:
#include <boost/serialization/base_object.hpp>
#include "B.h"
classe C : public B {
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & boost::serialization::base_object<B>(*this);
ar & m_someOtherInt;
}
protected:
int m_someOtherInt;
/*....*/
}
這是我的方法來調用保存和加載功能:
SerializationManager.h:
#include <A.h>
#include <C.h>
#include <boost/serialization/export.h>
BOOST_CLASS_EXPORT(C);
class SerializationManager {
/*....*/
public:
bool save(string filename);
bool load(string filename);
protected:
A* m_classToSave;
}
SerializationManager.cpp:
#include "SerializationManager.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <fstream>
#include <sstream>
bool SerializationManager::save(string filemname)
{
std::ofstream outputFile(filemname);
assert(outputFile.good());
boost::archive::text_oarchive oTextArchive(outputFile);
oTextArchive << m_classToSave;
return true;
}
bool SerializationManager::load(string filename)
{
delete m_classToSave;
std::ifstream ifs(filename);
assert(ifs.good());
boost::archive::text_iarchive ia(ifs);
// restore the schedule from the archive
ia >> m_classToSave;
return true;
}
/* ... */
我弄清楚爲什麼boost不能用我寫的代碼生成序列化代碼。 但我嘗試了幾種方法來實現我的工作,但我沒有這樣做。 你有解決方案來使我的代碼工作。 – stefhane
歡迎使用stackoverflow。不要忘記[投票](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – sehe
啊。評論中增加了一個問題。稍後我會看看 – sehe