Boost :: serialize - 反序列化对象的向量?

时间:2012-12-17 15:40:28

标签: c++ serialization boost

我最近想要序列化一个包含对象向量的类。我在文档中读到,如果我包含#include< boost / serialization / vector.hpp>,我将能够通过一个简单的&运营商。不幸的是,尽管编译器同意构建序列化代码,但它不允许我编译反序列化测试用例。那可能有什么问题?

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

#include <fstream>

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

class B
{

        friend class boost::serialization::access;

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

        std::vector<A> a;
};

int main()
{
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        B b;
        ia >> b;

}

以下是编译器的错误:

> make testcase
g++ -ggdb -fpermissive    testcase.cpp   -o testcase
In file included from /usr/include/boost/archive/detail/oserializer.hpp:67:0,
                 from /usr/include/boost/archive/detail/interface_oarchive.hpp:23,
                 from /usr/include/boost/archive/detail/common_oarchive.hpp:22,
                 from /usr/include/boost/archive/basic_text_oarchive.hpp:32,
                 from /usr/include/boost/archive/text_oarchive.hpp:31,
                 from testcase.cpp:1:
/usr/include/boost/archive/detail/check.hpp: In instantiation of ‘void boost::archive::detail::check_const_loading() [with T = const std::vector<A>]’:
/usr/include/boost/archive/detail/iserializer.hpp:577:5:   required from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive; T = const std::vector<A>]’
/usr/include/boost/archive/detail/common_iarchive.hpp:66:9:   required from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = const std::vector<A>; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/basic_text_iarchive.hpp:65:9:   required from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = const std::vector<A>; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/text_iarchive.hpp:82:9:   required from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = const std::vector<A>; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:60:9:   required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = const std::vector<A>; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:67:35:   [ skipping 9 instantiation contexts ]
/usr/include/boost/archive/detail/iserializer.hpp:592:5:   required from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive; T = B]’
/usr/include/boost/archive/detail/common_iarchive.hpp:66:9:   required from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = B; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/basic_text_iarchive.hpp:65:9:   required from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = B; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/text_iarchive.hpp:82:9:   required from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = B; Archive = boost::archive::text_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:60:9:   required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = B; Archive = boost::archive::text_iarchive]’
testcase.cpp:35:8:   required from here
/usr/include/boost/archive/detail/check.hpp:162:1: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’

1 个答案:

答案 0 :(得分:3)

serialize成员函数不应该是const。它用于序列化,但也用于反序列化。