2012-06-07 76 views
3

我想/使用Boost下面的矢量對象的值(不是指針)反序列化序列化:矢量的指針。 BOOST系列化

std :: vector <A*> m_vector; 

要序列我使用下面的代碼:

int nItems = m_vector.size(); 
ar & nItems; 
std::for_each(m_vector.begin(), m_vector.end(), [&ar](A* pItem) { 
    ar & *pItem; 
}); 

而對於反序列化:

int nItems; 
ar & nItems; 
for (int i = 0; i < nItems; ++i) { 
    A* pItem; 
    ar & *pItem; ///////////// Run-Time Check Failure #3 
    m_vector.push_back(pItem); 
} 

但是當我運行該程序,我得到以下錯誤:

Run-Time Check Failure # 3 - The variable 'pItem' is Being Used without Being initialized. 

我在做什麼錯?

謝謝。

回答

4

您需要爲對象分配內存指向pItem

A* pItem = new A; 
ar & *pItem; 
m_vector.push_back(pItem); 

錯誤是因爲雖然你有一個指針,有在表示指針指向的內存位置沒有對象 - 指針的值是垃圾(未初始化的指針)。

當您不再需要向量中指針指向的對象時,請不要忘記調用delete以防止內存泄漏。更好的是,使用智能指針(例如boost::shared_ptr<>)確保內存在不再可訪問時解除分配。

+0

謝謝您的幫助。 –

+1

@JoanCarles - 如果它幫助你解決問題,請接受答案 – Attila

1

2年後,但值得一提。

有一個更好的解決方案,用於序列化指向對象或任何其他STL容器(列表,集合等)的指針向量。 爲了序列的載體,添加:

#include <boost/serialization/vector.hpp> 

,然後你需要實現序列化()方法和你的朋友用archieve類。 一切都在這個例子中(仔細閱讀所有的意見,他們是非常重要的)解釋說:

#include <fstream> 
#include <iostream> 
#include <vector> 
#include <iostream> 

#include <boost/archive/text_oarchive.hpp> 
#include <boost/archive/text_iarchive.hpp> 
#include <boost/serialization/vector.hpp> 


class Abc{ 
    // In order to make Abc serializable 
    // you need to friend this lass with serialization::access 
    friend class boost::serialization::access; 

    // and then add this method 
    template<class Archive> 
    void serialize(Archive & ar, const unsigned int version) 
    { 
     // choose what class fields do you want to serialize 
     ar & a; 
     ar & b; 
     ar & c; 
    } 
public: 
    int a; 
    int b; 
    int c; 

    // don't forget about default constructor! It's necessary for serialization! 
    Abc(){}; 
    Abc(int a, int b, int c): a(a), b(b), c(c){}; 
}; 

class GpsPosition 
{ 
private: 
    // as mentioned above... 
    friend class boost::serialization::access; 
    template<class Archive> 
    void serialize(Archive & ar, const unsigned int version) 
    { 
     ar & degrees; 
     ar & minutes; 
     ar & seconds; 
     ar & wektorIntow; 
    } 
    int degrees; 
    int minutes; 
    float seconds; 
public: 
    std::vector<Abc*> abcVector; 
    GpsPosition(){}; 
    GpsPosition(int d, int m, float s): degrees(d), minutes(m), seconds(s) 
    { 
     // adding some objects to abcVector 
     abcVector.push_back(new Abc(1, 2, 3)); 
     abcVector.push_back(new Abc(3, 2, 3)); 
     abcVector.push_back(new Abc(2, 2, 3)); 
     abcVector.push_back(new Abc(1, 2, 3)); 
    } 
    int getDegrees(){ return this->degrees; } 
    int getMinutes(){ return this->minutes; } 
    float getSeconds(){ return this->seconds; } 
}; 

int main(){ 

    // And now how to use it 

    // Saving to file: 
    std::ofstream fileHandler("filename"); 
    const GpsPosition position1(35, 59, 24.567f); 
    { 
     boost::archive::text_oarchive boostOutputArchieve(fileHandler); 
     boostOutputArchieve << position1; 
    } 

    // Reading from file: 
    GpsPosition newPosition; 
    { 
     std::ifstream fileHandler; 
     try{ 
      fileHandler.open("filenacme"); 
      boost::archive::text_iarchive boostInputArchieve(fileHandler); 
      // read class state from archive 
      boostInputArchieve >> newPosition; 
      // archive and stream closed when destructors are called 
      fileHandler.close(); 
      } 
     catch (std::ifstream::failure e) { 
      std::cerr << "Exception opening/reading/closing file"; 
     } 
     catch(boost::archive::archive_exception e){ 
      std::cerr << "Exception opening/reading/closing file"; 
     } 
    } 

    // print to the console 
    std::cout << newPosition.getMinutes() << std::endl; 
    std::cout << newPosition.abcVector[0]->a; 
    std::cin.get(); 

    return 0; 
} 

欲瞭解更多信息,請查看本教程: http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html

+0

我想「wektorIntow」和「abcVector」應該有相同的名字? – Maverobot