2014-05-13 71 views
2

我試圖序列具有類成員變量一樣的std :: list類> LSS如何使用boost系列化

#include <boost/serialization/optional.hpp> 
#include <boost/serialization/map.hpp> 
#include <boost/serialization/shared_ptr.hpp> 
#include <boost/serialization/serialization.hpp> 
#include <boost/serialization/list.hpp> 

struct SomeStruct 
{ 
    int x; 
    double y; 

    private: 
     friend class boost::serialization::access; 
     template<class Archive> 
     void serialize(Archive &at, const unsigned int version) 
     { 
     at & x; 
     at & y; 
     } 

}; 

class BuildTest 
{ 

    std::list<boost::shared_ptr<SomeStruct *>> lss ; 
    std::string name; 

    private: 
     friend class boost::serialization::access; 
     template<class Archive> 
     void serialize(Archive &at, const unsigned int version) 
     { 
     at & lss; // is this sufficient to serialize lss ? 
     at & name; 
     } 

} 

我已經添加了所有需要的頭序列化類型的shared_ptr指針類型的列表升壓序列化文件。有類似的問題,但通過適當的答案how do i serialize list variable of type <boost::shared_ptr<void *>>

+0

請出示頭。你是否包含'boost/serialization/list.hpp',boost/serialization/shared_ptr.hpp'? –

+0

@DieterLücking - 添加代碼中使用的所有頭文件。 – manu

+0

是否爲SomeStruct編寫了序列化代碼? –

回答

1

你想

std::list<boost::shared_ptr<SomeStruct>> lss ; 

std::list<boost::shared_ptr<SomeStruct*>>

看到它Live On Coliru

#include <boost/serialization/optional.hpp> 
#include <boost/serialization/map.hpp> 
#include <boost/serialization/shared_ptr.hpp> 
#include <boost/serialization/serialization.hpp> 
#include <boost/serialization/list.hpp> 

#include <boost/archive/text_oarchive.hpp> 
#include <boost/archive/text_iarchive.hpp> 
#include <boost/make_shared.hpp> 

struct SomeStruct { 
    int x; 
    double y; 

private: 
    friend class boost::serialization::access; 
    template<class Archive> 
    void serialize(Archive &at, const unsigned int version) { 
     at & x; 
     at & y; 
    } 

}; 

class BuildTest { 
    public: 

    std::list<boost::shared_ptr<SomeStruct>> lss ; 
    std::string name; 

    private: 
    friend class boost::serialization::access; 
    template<class Archive> 
    void serialize(Archive &at, const unsigned int version) { 
     at & lss; // is this sufficient to serialize lss ? 
     at & name; 
    } 

}; 

int main() { 
    BuildTest bt; 
    bt.name = "Let's try this"; 
    bt.lss = { boost::make_shared<SomeStruct>(), boost::make_shared<SomeStruct>() }; 
    boost::archive::text_oarchive oa(std::cout); 
    oa << bt; 
} 
+0

爲什麼我們不能使用std :: list > – manu

+0

@manu爲什麼/你會如何使用SomeStruct **?你將如何分配這個?我會說這是浪費和無用或者一個非常糟糕的主意。 (可能兩者)。 – sehe

相關問題