【问题标题】:Serialization of Class that has serializable nested classes in it strange compilation error具有可序列化嵌套类的类的序列化奇怪的编译错误
【发布时间】:2011-12-10 20:52:57
【问题描述】:

我正在尝试学习一些 boost 序列化的基础知识。所以我关注the tutorial 并创建了简单的class Aclass Bclass C,其中有A a_;B b_; 作为私有成员。

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

class A{
private:
        friend class boost::serialization::access;

        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar & a_;
        }

        int a_;

public:
        A(){ std::cout << "A constructed" << std::endl; }
        A(int a): a_(a) { std::cout << "A constructed with 'a' ==" << a << std::endl; }
};
class B{
private:
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar & b_;
        }

        std::string b_;
public:
        B(){ std::cout << "B constructed" << std::endl; }
        B(std::string b): b_(b) { std::cout << "B constructed with 'b' ==" << b << std::endl; }
};

class C{
private:
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar & a_;
                ar & b_;
                ar & d_;
        }

        A a_;
        B b_;
        double d_;

public:
        C(){  std::cout << "C constructed" << std::endl; }
        C(int a, std::string b, double d ): a_(a), b_(b), d_(d) { std::cout << "C constructed with 'd' == " << d << std::endl; }
};

int main() {
    // create and open a character archive for output
    std::ofstream ofs("filename");

    // create class instance
    C c(15, "rock and roll", 25.8);

    // save data to archive
    {
            boost::archive::text_oarchive oa(ofs);
            // write class instance to archive
            oa << c;
            // archive and stream closed when destructors are called
    }

    C c_recreated;
    {
            // create and open an archive for input
            std::ifstream ifs("filename");
            boost::archive::text_iarchive ia(ifs);
            // read class state from archive
            ia >> c_recreated;
            // archive and stream closed when destructors are called
    }

    std::cin.get();
}

在 IDEone live 中,它是 here,带有所有奇怪和可怕的编译器错误。在我的 VS2010 上,我只收到 2 个相同的错误:

Error   2   error C2248: 'C::serialize' : cannot access private member declared in class 'C'
Error   3   error C2248: 'C::serialize' : cannot access private member declared in class 'C'    

我做错了什么,在拥有class Aclass B 之后如何使class C 可序列化?

【问题讨论】:

    标签: c++ boost boost-serialization


    【解决方案1】:

    对于BC,您没有friend class boost::serialization::access;

    【讨论】:

    • 添加here后在IDEone上仍然无法编译,为什么,这是否意味着这段代码在linux上无法编译,如何修复IDEone编译?在我的VS上完美编译,非常感谢!!!)))
    • @myWallJSON:好像 Ideone 只提供了 Boost 的头文件,并且没有像 Boost.Serialize 这样的东西的编译库。这就是链接器抱怨的原因。
    猜你喜欢
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多