2014-01-24 61 views
1

我遇到以下情況的問題: 我有一個類,讓我們說一個動態庫的基礎。基地可以被序列化。 我有另一個類派生,在不同的動態庫,這是從基地,也可以序列化派生。 當我序列化存儲在與libBase和libDerived鏈接的程序中的Base *變量的Derived實例時,我無法使提升看到Derived。產生 即多庫和增強序列化

該文件具有以下內容

22 serialization::archive 10 0 1 0 
0 10 Base Class 

據我瞭解,派生的序列化功能則永遠不會調用

我缺少什麼?

Base.h

#ifndef BASE_H 
#define BASE_H 
#include <string> 
#include <boost/serialization/export.hpp> 
#include <boost/serialization/access.hpp> 
using namespace std; 
class Base { 
public: 
    Base(); 
private: 
    string _str; 
    friend class boost::serialization::access; 
    template<class Archive> 
    void serialize(Archive & ar, const unsigned int version); 
}; 
BOOST_CLASS_EXPORT_KEY(Base) 
#endif 

Base.cpp

#include "base.h" 
Base::Base() : _str("Base Class") {} 
template<class Archive> 
void Base::serialize(Archive & ar, const unsigned int version){ 
    ar & _str; 
} 

#include <boost/archive/text_oarchive.hpp> 
#include <boost/archive/text_iarchive.hpp> 
template void Base::serialize(boost::archive::text_oarchive & ar, const unsigned int version); 
template void Base::serialize(boost::archive::text_iarchive & ar, const unsigned int version); 
BOOST_CLASS_EXPORT_IMPLEMENT(Base) 

Derived.h

#ifndef DERIVED_H 
#define DERIVED_H 
#include <string> 
#include "base.h" 
#include <boost/serialization/export.hpp> 
#include <boost/serialization/access.hpp> 
using namespace std; 
class Derived : public Base { 
public: 
    Derived(); 
private: 
    string _str; 
    friend class boost::serialization::access; 
    template<class Archive> 
    void serialize(Archive & ar, const unsigned int version); 
}; 
BOOST_CLASS_EXPORT_KEY(Derived) 
#endif 

Derived.cpp

#include "derived.h" 
#include <boost/serialization/base_object.hpp> 
Derived::Derived() : Base(), _str("derived") { } 

template<class Archive> 
void Derived::serialize(Archive & ar, const unsigned int version){ 
    ar & boost::serialization::base_object<Base>(*this); 
    ar & _str; 
} 

#include <boost/archive/text_oarchive.hpp> 
#include <boost/archive/text_iarchive.hpp> 
template void Derived::serialize(boost::archive::text_oarchive & ar, const unsigned int  version); 
template void Derived::serialize(boost::archive::text_iarchive & ar, const unsigned int version); 
BOOST_SERIALIZATION_MWERKS_BASE_AND_DERIVED(Base, Derived) 
BOOST_CLASS_EXPORT_IMPLEMENT(Derived) 

main.cpp中

#include "derived.h" 
#include <iostream> 
#include <fstream> 
#include <boost/archive/text_oarchive.hpp> 
int main() 
{ 
    Base * b = new Derived(); 
    std::ofstream ofs("test.txt"); 
    boost::archive::text_oarchive oa(ofs); 
    oa << b; 
    ofs.close(); 
} 

compile.sh

g++ -g -c -Wall -fPIC base.cpp -o base.o 
g++ -shared -Wl,-soname,libbase.so -o libbase.so base.o -lc 
g++ -g -c -Wall -fPIC derived.cpp -o derived.o 
g++ -shared -Wl,-soname,libderived.so -o libderived.so derived.o -lc 
g++ main.cpp -lbase -lderived -o main -L . -lboost_serialization 
+0

什麼是症狀嗎? 「我不能讓提振看到X」並沒有告訴我任何事情。此外,它在我的盒子上工作。在某種意義上,它不會出錯 – sehe

+0

Derived的序列化函數從不被調用。我剛剛編輯了這個問題。 –

回答

2

你的類型需要支持RTTI。

爲了這是真實的,它需要有一個虛擬表。最常見的成員,使虛擬的析構函數:

virtual ~Base() = default; 

現在它導致

22 serialization::archive 10 1 1 0 
0 1 0 
1 10 Base Class 13 derived_value 

如果你不能使用RTTI,該庫提供了一個選擇:http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/traits.html#typeinfo

+1

查看** [Live On Coliru](http://coliru.stacked-crooked.com/a/ec0adc4abf3c9c32)**和要點https://gist.github.com/sehe/8608881 – sehe