2012-01-04 74 views
1

我在查看代碼來組織論文。我有兩個類(作者和紙),通過遏制和指針相互關聯。我定義了兩個類的序列化,但是如果我放棄關於另一個類的信息,我只能保存一個對象。我完全不熟悉對象序列化,並且我覺得我已經走得儘可能遠了。加載指向對象的指針(C++ - linux)

Author.h

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

class Paper; 

class Author 
{ 
    public: 
     friend class Paper; 

     Author(const std::string& last_name_in = "", 
     const std::string& first_name_in = "", 
     const std::string& middle_name_in = ""); 
     ... 

    private: 
     friend class boost::serialization::access; 
     template<class Archive> 
     void serialize(Archive & ar, const unsigned int version) 
     { 
      ar & first_name; 
      ar & last_name; 
      ar & middle_name; 
      ar & display_name; 
      for (int ii = 0; ii < list_of_papers.size(); ii++) 
       ar & list_of_papers[ii]; 
     } 
     ... 
     std::string first_name; 
     std::string last_name; 
     std::string middle_name; 
     std::string display_name; 
     std::vector<const Paper*> list_of_papers; 
}; 

我沒有在Paper.h一個默認的構造,因爲它沒有任何意義,我真的不知道我會怎麼去用它。 Paper.h

... 
class Author; 

class Paper 
{ 
    public: 
     friend class Author; 
     Paper(const std::string& paper_title_in, 
     const std::vector<Author*>& paper_authors_in); 
     ... 

    private: 
     friend class boost::serialization::access; 
     template<class Archive> 
     void serialize(Archive & ar, const unsigned int version) 
     { 
      ar & paper_title; 
      for (int ii = 0; ii < paper_authors.size(); ii++) 
       ar & paper_authors[ii]; 
     } 

     std::string   paper_title; 
     std::vector<Author*> paper_authors; 
}; 

TEST.CPP

int main() 
{ 
    std::ofstream ofs("filename"); 
    { 
     Author author1("Doe", "John"); 
     Author author2("Dude", "Jim", "J"); 
     std::vector<Author*> list_of_authors; 
     list_of_authors.push_back(&author1); 
     Paper other_paper("A Nice Paper", list_of_authors); 
     list_of_authors.push_back(&author2); 
     Paper this_is_my_paper("What a beautiful paper", list_of_authors); 
     boost::archive::text_oarchive oa(ofs); 
     oa << author1; 
    } 
    { 
     Author author_new; 
     std::ifstream ifs("filename"); 
     boost::archive::text_iarchive ia(ifs); 
     ia >> author_new; 
    } 

    return 0; 
} 

在Linux中,我編譯克++:

g++ -o test -I /path/boost/ -L /path/boost/ -lboost_serialization test.cpp author.cpp paper.cpp 

在形式上面提出,碼不會編譯並給我一個巨大的信息。它會在我添加對象序列化之前進行編譯,並在編譯時刪除'ar & list_of_papers [ii];'來自Author.h。

任何人都可以看到我失蹤?

+0

我想你沒有提供完整的路徑到您的升壓庫 – Arunmu 2012-01-04 15:07:18

+0

你可以張貼錯誤消息 – Lars 2012-01-04 16:28:30

回答

0
ar & paper_authors; 

應直接工作,因爲提升可序列化提供的對象本身是serialisable 比照對象的STL容器:http://www.boost.org/doc/libs/1_35_0/libs/serialization/doc/tutorial.html#stl

+0

paper_authors 是作者指針的向量。它不會改變什麼嗎? – Kroust 2012-01-05 13:36:10

+0

我做了@lezebulon建議的修改。我還添加了 #include 最後,我將list_of_papers從 更改爲std :: vector list_of_papers; 至 std :: vector list_of_papers; 和一切工作正常。這是否意味着不能保存/加載const指針?還是需要更精煉的技巧? 無論如何,感謝您的幫助。 – Kroust 2012-01-05 14:18:54