使用私有构造函数的Boost序列化工作在1.57而不是1.58

时间:2015-04-26 17:20:23

标签: serialization boost

在某些情况下,使用私有构造函数对类进行反序列化不适用于Boost 1.58。在1.57它工作得很好。使用Visual Studio 2013编译时,下面的代码会显示以下错误消息。

错误消息:

1>  SerializeTest.cpp
1>c:\program files (x86)\vc\include\xmemory0(588): error C2248: 'A::A' : cannot access private member declared in class 'A'
1>          c:\projects\serializetest\serializetest.cpp(14) : see declaration of 'A::A'
1>          c:\projects\serializetest\serializetest.cpp(9) : see declaration of 'A'

代码:

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

class A
{
public:
    A(int i) {}

private:
    A() {}
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version)
    {
    }
};

class B
{
public:
    B() {}

private:
    std::vector<A> m_a;

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(m_a);
    }
};


int main(int argc, char* argv[])
{
    std::ostringstream os;
    boost::archive::text_oarchive oa(os);
    {
        B b;
        oa & b;
    }

    {
        std::stringstream myIstream;
        myIstream.write(os.str().c_str(), os.str().length());
        boost::archive::text_iarchive ia(myIstream);

        B b;
        ia >> b;
    }

    return 0;
}

这是Boost回归还是我的疏忽?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,请查看xmemory标题!对我来说错误出现在std :: allocator上,所以我需要添加像

这样的东西
192.168.1.40 s5 s4.hadoop.ipin.com
192.168.1.50 s5 s5.hadoop.ipin.com
192.168.1.60 s6 s6.hadoop.ipin.com
192.168.1.91 data91 data1

除了序列化的朋友类!