【问题标题】:Boost with vectors of pointers to object (C++ - linux)使用指向对象的指针向量提升 (C++ - linux)
【发布时间】:2012-02-02 11:27:43
【问题描述】:

我正在查看用于组织论文的代码。我有两个类(Author 和 Paper),它们通过包含和指针相互连接。我为这两个类定义了序列化,但是如果我丢弃有关另一个类的信息,我只能保存一个对象。我对对象序列化完全陌生,我觉得我已经走到了尽头。

作者.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++ 编译:

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

在上面显示的表单中,代码无法编译并给我一个巨大的信息。它会在我添加对象序列化之前编译,如果我删除 'ar & list_of_papers[ii];' 就会编译来自作者.h。

谁能看到我错过了什么?

【问题讨论】:

  • 我认为你没有提供你的 boost 库的完整路径
  • 你能把错误信息贴出来

标签: c++ linux serialization boost


【解决方案1】:
ar & paper_authors;

应该直接工作,因为 boost 可以序列化对象的 STL 容器,前提是对象本身是可序列化的 参考:http://www.boost.org/doc/libs/1_35_0/libs/serialization/doc/tutorial.html#stl

【讨论】:

  • paper_authors 是指向作者的指针向量。没有什么改变吗?
  • 我做了@lezebulon 建议的修改。我还添加了 #include 最后,我将 list_of_papers 从 std::vector list_of_papers;到 std::vector list_of_papers;一切正常。这是否意味着无法保存/加载 const 指针?还是需要更精致的技巧?无论如何感谢您的帮助。
猜你喜欢
  • 2011-10-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
相关资源
最近更新 更多