【问题标题】:Cereal: Serializing polymorphic typeCereal:序列化多态类型
【发布时间】:2016-03-09 11:11:13
【问题描述】:

我在序列化多态类型时遇到问题。实际上,我只是将示例拆分为:http://uscilab.github.io/cereal/polymorphism.html 几个文件。它编译得很好,但在运行时我得到一个异常,告诉我我无法序列化在代码中到达这一行时未注册的多态类型: 存档(ptr1,ptr2); 它应该将 ptr1 和 ptr2 的内容序列化为流。

我附上文件,以便任何人都可以看到发生了什么。

提前感谢您的宝贵时间! 最好的, 罗杰。

////////////// IBaseClass.h
    #ifndef _IBASECLASS_H_
    #define _IBASECLASS_H_

    // A pure virtual base class
    class IBaseClass
    {
    public:
      virtual void sayType() = 0;
    };

    #endif

    ////////////// DerivedClass.h
    #ifndef DERIVEDCLASS_H_
    #define DERIVEDCLASS_H_

    #include "IBaseClass.h"

    #include <cereal/types/polymorphic.hpp>

    class DerivedClass : public IBaseClass {
        void sayType();

        int x;

        template<class Archive>
        void serialize( Archive & ar )
        { ar( x ); }
    };

    #include <cereal/archives/binary.hpp>
    #include <cereal/archives/xml.hpp>
    #include <cereal/archives/json.hpp>

    // Register DerivedClassOne
    CEREAL_REGISTER_TYPE(DerivedClass);

    #endif /* DERIVEDCLASS_H_ */

    ////////////// DerivedClass2.h
    #ifndef DERIVEDCLASS2_H_
    #define DERIVEDCLASS2_H_

    #include "IBaseClass.h"

    #include <cereal/types/polymorphic.hpp>

    class DerivedClass2 : public IBaseClass {
        void sayType();

        float y;

        template<class Archive>
        void serialize( Archive & ar )
        { ar( y ); }
    };

    #include <cereal/archives/binary.hpp>
    #include <cereal/archives/xml.hpp>
    #include <cereal/archives/json.hpp>

    CEREAL_REGISTER_TYPE(DerivedClass2);

    ////////////// main.cpp
    #include "DerivedClass.h"
    #include "DerivedClass2.h"
    #include <iostream>
    #include <fstream>
    #include <memory>

    #include <cereal/archives/xml.hpp>
    #include <cereal/types/polymorphic.hpp>

    int main(int argc, char* argv[])
    {
        {
            std::ofstream os( "polymorphism_test.xml" );
            cereal::XMLOutputArchive oarchive( os );

            // Create instances of the derived classes, but only keep base class pointers
            std::shared_ptr<IBaseClass> ptr1 = std::make_shared<DerivedClass>();
            std::shared_ptr<IBaseClass> ptr2 = std::make_shared<DerivedClass2>();
            oarchive( ptr1, ptr2 );
          }

          {
            std::ifstream is( "polymorphism_test.xml" );
            cereal::XMLInputArchive iarchive( is );

            // De-serialize the data as base class pointers, and watch as they are
            // re-instantiated as derived classes
            std::shared_ptr<IBaseClass> ptr1;
            std::shared_ptr<IBaseClass> ptr2;
            iarchive( ptr1, ptr2 );

            // Ta-da! This should output:
            ptr1->sayType();  // "DerivedClassOne"
            ptr2->sayType();  // "EmbarrassingDerivedClass. Wait.. I mean DerivedClassTwo!"
          }

          return 0;
    }

【问题讨论】:

  • 您使用的是哪个编译器/操作系统?你能粘贴你编译时使用的确切命令吗?

标签: c++ serialization cereal


【解决方案1】:

https://uscilab.github.io/cereal/polymorphism.html

由于您没有对 grain::base_class(this) 进行任何序列化,因此没有从派生类到基类的路径。尝试添加:

CEREAL_REGISTER_POLYMORPHIC_RELATION(BaseClass, DerivedClassOne) CEREAL_REGISTER_POLYMORPHIC_RELATION(BaseClass, EmbarrassingDerivedClass)

【讨论】:

  • 有点老对 OP 有帮助,但如果有人在搜索,这可能会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 2019-09-18
  • 2014-05-01
  • 2020-09-14
  • 1970-01-01
相关资源
最近更新 更多