2016-11-17 13 views
2

ブーストシリアル化を使用して複数のクラスを格納およびロードしています。私の目標は、他のクラスを含むあるクラスで直列化を使用することで、他のクラスは直列化されます。使用順方向宣言クラスと継承でのブーストシリアル化

しかし、これらのクラスには前方宣言と継承が含まれているため、これらのクラスでの昇順シリアル化を行うことはできません。

私はこれらのエラーで、特に前方宣言で、コンパイル時に問題を抱えている:

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:

#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; 

/*....*/ 

} 

章:

ベクトルm_BvectorはB *または/およびC *は

オブジェクトBhとを含むことができ

#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; /*....*/ 

}

そして、これはsaveとload関数を呼び出すための私の方法です:

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; 
} 

/* ... */ 

答えて

1

ブーストはタイプが仮想かどうか(通常はvtableで実装された仮想メソッドがある)を知る必要があるため、typeiddynamic_castを使用してランタイム忠実値を返す必要があります。

タイプの定義が利用可能になる前に、シリアル化の仕組みをインスタンス化しようとしています(タイプはforward-declaredの場合のみ不完全です)ので、シリアル化コードを生成できません。

+0

なぜ私が書いたものでシリアル化コードを生成できないのかを調べます。 しかし私は私の仕事を達成するためにいくつかの方法を試して、私はそれをやりませんでした。 私のコードを動作させるための解決策がありますか? – stefhane

+0

ようこそstackoverflowへ。 [投票](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)を忘れないでください – sehe

+0

ああ。コメントに質問が追加されました。私は後で見ていきます – sehe

関連する問題