谷物序列化错误 [英] Cereal serialization error

查看:145
本文介绍了谷物序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我很困惑.它不会为外部序列化功能进行编译.它给出了错误

So i'm legit confused. It won't compile for an external serialization function. It gives the error

谷物无法找到任何输出序列化函数 提供的类型和归档组合.

cereal could not find any output serialization functions for the provided type and archive combination.

因此下面的代码无法编译

So the code below doesn't compile

#include <fstream>
#include <glm/glm.hpp>
#include "SceneObject.h"
#include <cereal/cereal.hpp>
#include <cereal/archives/json.hpp>

template<typename Archive> void serialize(Archive& archive, glm::vec3& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

struct something
{
public:
    float x, y, z;
};
template<typename Archive> void serialize(Archive& archive, something& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

int main(int argc, char** argv)
{
    SceneObject test;
    test.transform().setPosition(1.0f,2.0f,3.0f);

    {
        std::ofstream file("TestPath.json");
        cereal::JSONOutputArchive output(file);
        glm::vec3 p = test.transform().getPosition();
        output(p);
    }

    return 0;
}

但这确实可以编译

#include <fstream>
#include <glm/glm.hpp>
#include "SceneObject.h"
#include <cereal/cereal.hpp>
#include <cereal/archives/json.hpp>

template<typename Archive> void serialize(Archive& archive, glm::vec3& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

struct something
{
public:
    float x, y, z;
};
template<typename Archive> void serialize(Archive& archive, something& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

int main(int argc, char** argv)
{
    SceneObject test;
    test.transform().setPosition(1.0f,2.0f,3.0f);

    {
        std::ofstream file("TestPath.json");
        cereal::JSONOutputArchive output(file);
        glm::vec3 p = test.transform().getPosition();
        something s;
        s.x = p.x;
        s.y = p.y;
        s.z = p.z;
        output(s);
    }

    return 0;
}

我从字面上将保存代码从glm :: vec3复制并粘贴到某些东西,然后将glm :: vec3更改为'something'.对我而言,它为什么对一个而不对另一个有用,这没有任何意义.我认为这可能是名称空间的事情,但是我不知道如何解决该问题.

I literally copy and pasted the save code from glm::vec3 to something and just changed glm::vec3 to 'something'. It makes NO sense to me why it would work for one and not the other. I think it might be a namespace thing, but I have no clue how to fix that.

推荐答案

显然,发布使我找到了解决方案.

Well apparently posting made me find the solution.

您需要确保序列化函数共享相同的名称空间,因此如果我像

You need to make sure the serialize functions share the same namespace so if I wrap them like

namespace glm
{
template<typename Archive> void serialize(Archive& archive, glm::vec3& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y),    cereal::make_nvp("z", v3.z));
}
}

有效.有点奇怪,但这就是事实.

It works. Kinda odd, but it is what it is.

这篇关于谷物序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆