c++ - 使用指向对象的指针 vector 进行 boost (C++ - linux)

标签 c++ linux serialization boost

我正在查看用于组织论文的代码。我有两个类(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;
};

测试.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。

有人能看到我错过了什么吗?

最佳答案

ar & paper_authors;

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

关于c++ - 使用指向对象的指针 vector 进行 boost (C++ - linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728952/

相关文章:

C++ 如何每 10 秒检查一次时间?

c++ - STL 排序保留原始顺序

mysql - 在 memcached 中存储不同案例类的列表

c++ - 将 boost::serialization::serialize'ble 结构作为二进制存储到硬盘上

java - 在 java 中序列化 Float 以供 C++ 应用程序读取的最佳方法?

c++ - 此类设置是否违反 Liskov 替换原则

c++ - 如何使用fstream c++替换文本文件中的特定值?

linux - auth=true 在 mongodb 32 位 Linux 上不工作

linux - shell 脚本 : How to read standard output of a program from console

linux - 我正在尝试按照以下模式获取组下的用户列表