3

当尝试用 序列化派生类时, (Boost 1.53.0boost::serialization中的第 436 行)中的断言被触发:boost/archive/detail/oserializer.hpp

const basic_pointer_oserializer * bpos
    = static_cast<const basic_pointer_oserializer *>(
        boost::serialization::singleton<
            archive_serializer_map<Archive>
        >::get_const_instance().find(*true_type)
    );
BOOST_ASSERT(NULL != bpos);

这是我的 MWE:

#include <iostream>
#include <fstream>

#include <boost/archive/polymorphic_xml_oarchive.hpp>

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>

class base
{
    friend class boost::serialization::access;

    template <class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & boost::serialization::make_nvp("test", test);
    }

    int test;

public:
    virtual void print() { std::cout << 1 << std::endl; }
};

class derived : public base
{
    friend class boost::serialization::access;

    template <class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & boost::serialization::make_nvp("base", boost::serialization::base_object<base>(*this));
        ar & boost::serialization::make_nvp("test2", test2);
    }

    int test2;

public:
    void print() { std::cout << 2 << std::endl; }
};

int main (int argc, char const *argv[])
{
    base *b = new derived();

    std::ofstream stream("out.xml");
    boost::archive::polymorphic_xml_oarchive ar(stream);
    ar.register_type<derived>();
    ar << boost::serialization::make_nvp("b", b);

    return 0;
}

我也尝试过使用BOOST_CLASS_EXPORT_GUID而不是register_type,但这也没有用。知道我做错了什么吗?

4

1 回答 1

2

您可能不是要使用polymorphic_xml_archive (请参阅文档)

值得注意的是,多态档案与档案的接口有关,与您要序列化的实体无关(它们总是可以是多态的)。

如果你xml_archive改用,问题就消失了。

于 2013-06-15T23:10:40.483 回答