其他分享
首页 > 其他分享> > c – 谷物序列化错误

c – 谷物序列化错误

作者:互联网

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

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

所以下面的代码不能编译

#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’.对我来说,为什么它对一个而不是另一个有效是没有意义的.我认为它可能是命名空间的东西,但我不知道如何解决这个问题.

解决方法:

很明显张贴让我找到了解决方案.

你需要确保序列化函数共享相同的命名空间,所以如果我将它们包装起来

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));
}
}

有用.有点奇怪,但它就是它.

标签:glm-math,cereal,c,serialization,save
来源: https://codeday.me/bug/20190824/1704965.html