c++ - "cereal::Exception"使用命名值对 (nvp) 反序列化多态类型时

标签 c++ inheritance polymorphism cereal

在尝试反序列化序列化命名值对的多态类时,我收到“ Cereal ::异常”消息:“JSON 解析失败 - 未找到 NVP”。 我做错了什么吗?

这是标题:

#ifndef SERIALIZATION_H
#define SERIALIZATION_H

struct ComflabulationBase {
    ComflabulationBase(float thingy = 0.0f, int dingy = 0.0f, bool mingy = false, const std::string& stringy = "") : thingy(thingy), dingy(dingy), mingy(mingy), stringy(stringy) {}
    float thingy;
    int dingy;
    bool mingy;
    std::string stringy;

    template <class Archive>
    void serialize(Archive & ar)
    {
        ar(
            CEREAL_NVP(thingy),
            CEREAL_NVP(dingy),
            CEREAL_NVP(mingy),
            CEREAL_NVP(stringy)
          );
    }
    virtual ~ComflabulationBase() {}
    virtual void sayType() { std::cout << "Base" << std::endl; }
};

struct Vector3
{
    float x{0};
    float y{0};
    float z{0};

    template <class Archive>
    void serialize(Archive & ar)
    {
        ar(
            CEREAL_NVP(x),
            CEREAL_NVP(y),
            CEREAL_NVP(z)
          );
    }
};

struct Comflabulation : public ComflabulationBase
{
    Comflabulation(float bazingy = 1.5f, Vector3 vingy = Vector3()) : bazingy(bazingy), vingy(vingy) {}
    ~Comflabulation() {}
    float bazingy;
    Vector3 vingy;

    template <class Archive>
    void serialize(Archive & ar)
    {
        ar( cereal::base_class<ComflabulationBase>( this ),
            CEREAL_NVP(bazingy),
            CEREAL_NVP(vingy)
          );
    }

    void sayType() {std::cout << "Derived" << std::endl; }

};
CEREAL_REGISTER_TYPE(Comflabulation)

#endif // SERIALIZATION_H

和我的 cpp 文件:

#include "cereal/cereal.hpp"
#include "cereal/archives/binary.hpp"
#include "cereal/archives/json.hpp"
#include "cereal/types/polymorphic.hpp"
#include <sstream>

#include "serialization.h"


template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}

int main (void)
{
    std::stringstream ss;

    {
        std::unique_ptr<Comflabulation> one = make_unique<Comflabulation>();
        std::unique_ptr<Comflabulation> two = make_unique<Comflabulation>(3.0f);
        std::unique_ptr<Comflabulation> three = make_unique<Comflabulation>();
        cereal::JSONOutputArchive oarchive(ss);
        oarchive(one, two, three);
    }

    std::cout << ss.str() << std::endl;

    {
       std::unique_ptr<ComflabulationBase> one, two, three;
       cereal::JSONInputArchive iarchive(ss);
       iarchive(two, one, three);
       two->sayType();
    }


    std::cout << ss.str() << std::endl;

    return 0;
}

最佳答案

看来您需要序列化指向多态基类的智能指针(参见 http://uscilab.github.io/cereal/polymorphism.html ):

std::unique_ptr<ComflabulationBase> one = make_unique<Comflabulation>();
std::unique_ptr<ComflabulationBase> two = make_unique<Comflabulation>(3.0f);
std::unique_ptr<ComflabulationBase> three = make_unique<Comflabulation>();

感谢 Manuel Schiller 指出这一点。

关于c++ - "cereal::Exception"使用命名值对 (nvp) 反序列化多态类型时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081403/

相关文章:

c++ - 如何将 MinGW bin 目录添加到我的系统路径?

C++ 无法实例化抽象类子类

java - 数组有问题,无法向他添加任何内容

c++ - 内存管理 : scope and local pointer variable

c++ - 从 64 位无符号整数初始化 __m128 类型

c++ - VS C++ 编译器应该如何处理这段代码?

c++:将指针传递给基类时访问派生类方法?

javascript - ExtJS:Ext.Window Prototypal继承的对象无法被销毁

C++ 重载函数和子类

c++ - 如何正确重写虚方法来添加功能?