- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试创建一个具有静态函数的可序列化基类,以序列化和反序列化派生类对象。我已经阅读了用于注册多态关系的 Cereal 文档以及如何注册类型以及如何声明序列化函数。目标是像这样使用 Serializable:
std::stringstream& ss Serializable::serialize(test);
我使用的是 Visual Studio 平台工具 2017(v141)。目标 Windows SDK 是 10.0.17134.0
但我无法构建我的应用程序并得到这 2 个错误 3 倍:
Error C2338 cereal could not find any output serialization functions for the provided type and archive combination.
Error C2338 cereal could not find any input serialization functions for the provided type and archive combination.
这是我的代码:
序列化.hpp#pragma once
#include <string>
#include <cereal/archives/portable_binary.hpp>
class Serializable
{
public:
Serializable() = default;
~Serializable() = default;
virtual bool isAccessible() = 0;
static std::stringstream serialize(std::shared_ptr<Serializable> serializable);
static std::shared_ptr<Serializable> deserialize(std::stringstream& serialized);
};
序列化.cpp
#include "Serializable.hpp"
std::stringstream Serializable::serialize(std::shared_ptr<Serializable> serializable)
{
std::stringstream ss;
{
cereal::PortableBinaryOutputArchive ar(ss);
ar << serializable;
}
return ss;
}
std::shared_ptr<Serializable> Serializable::deserialize(std::stringstream& serialized)
{
cereal::PortableBinaryInputArchive ar(serialized);
std::shared_ptr<Serializable> result = nullptr;
ar >> result;
return result;
}
测试对象.hpp
#pragma once
#include "Serializable.hpp"
#include <string>
#include <cereal/types/string.hpp>
#include <cereal/types/polymorphic.hpp>
#include <cereal/types/base_class.hpp>
class TestObject : public Serializable
{
public:
TestObject() = default;
TestObject(const std::string& name);
~TestObject() = default;
std::string getName() const { return this->name; };
template<class Archive>
void serialize(Archive& ar)
{
ar(cereal::base_class<Serializable>(this), name);
};
virtual bool isAccessible() {
return true;
};
private:
std::string name;
};
CEREAL_REGISTER_TYPE(TestObject)
CEREAL_REGISTER_POLYMORPHIC_RELATION(Serializable,TestObject)
测试对象.cpp
#include "TestObject.hpp"
TestObject::TestObject(const std::string& name)
:TestObject(name){}
main.cpp
#include "TestObject.hpp"
#include "Serializable.hpp"
#include <memory>
#include <iostream>
#include <string>
int main(int argc, char **argv)
{
std::shared_ptr<Serializable> test(new TestObject("Test"));
auto ss = Serializable::serialize(test);
std::shared_ptr<Serializable> deserialized = Serializable::deserialize(ss);
auto test2 = dynamic_cast<TestObject*>(deserialized.get());
std::cout << test2->getName();
system("timeout 3");
return 0;
}
这是完整的构建错误:
1>------ Build started: Project: SerializableTest, Configuration: Debug Win32 ------
1>TestObject.cpp
1>Serializable.cpp
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(462): error C2338: cereal could not find any output serialization functions for the provided type and archive combination.
1>
1> Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these).
1> Serialize functions generally have the following signature:
1>
1> template<class Archive>
1> void serialize(Archive & ar)
1> {
1> ar( member1, member2, member3 );
1> }
1>
1>
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(347): note: see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,1>::processImpl<std::shared_ptr<Serializable>,0>(const T &)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryOutputArchive,
1> T=std::shared_ptr<Serializable>
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(347): note: see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,1>::processImpl<std::shared_ptr<Serializable>,0>(const T &)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryOutputArchive,
1> T=std::shared_ptr<Serializable>
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(290): note: see reference to function template instantiation 'void cereal::OutputArchive<cereal::PortableBinaryOutputArchive,1>::process<std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> T=std::shared_ptr<Serializable> &
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(290): note: see reference to function template instantiation 'void cereal::OutputArchive<cereal::PortableBinaryOutputArchive,1>::process<std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> T=std::shared_ptr<Serializable> &
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\serializable.cpp(8): note: see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,1>::operator <<<std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryOutputArchive,
1> T=std::shared_ptr<Serializable> &
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\serializable.cpp(8): note: see reference to function template instantiation 'ArchiveType &cereal::OutputArchive<ArchiveType,1>::operator <<<std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryOutputArchive,
1> T=std::shared_ptr<Serializable> &
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(851): error C2338: cereal could not find any input serialization functions for the provided type and archive combination.
1>
1> Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these).
1> Serialize functions generally have the following signature:
1>
1> template<class Archive>
1> void serialize(Archive & ar)
1> {
1> ar( member1, member2, member3 );
1> }
1>
1>
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(730): note: see reference to function template instantiation 'ArchiveType &cereal::InputArchive<ArchiveType,1>::processImpl<std::shared_ptr<Serializable>,0>(const T &)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryInputArchive,
1> T=std::shared_ptr<Serializable>
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(730): note: see reference to function template instantiation 'ArchiveType &cereal::InputArchive<ArchiveType,1>::processImpl<std::shared_ptr<Serializable>,0>(const T &)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryInputArchive,
1> T=std::shared_ptr<Serializable>
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(660): note: see reference to function template instantiation 'void cereal::InputArchive<cereal::PortableBinaryInputArchive,1>::process<std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> T=std::shared_ptr<Serializable> &
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\lib\cereal\include\cereal\cereal.hpp(660): note: see reference to function template instantiation 'void cereal::InputArchive<cereal::PortableBinaryInputArchive,1>::process<std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> T=std::shared_ptr<Serializable> &
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\serializable.cpp(17): note: see reference to function template instantiation 'ArchiveType &cereal::InputArchive<ArchiveType,1>::operator >><std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryInputArchive,
1> T=std::shared_ptr<Serializable> &
1> ]
1>k:\programme\c++\serializabletest\networkdebugs\serializabletest\serializable.cpp(17): note: see reference to function template instantiation 'ArchiveType &cereal::InputArchive<ArchiveType,1>::operator >><std::shared_ptr<Serializable>&>(T)' being compiled
1> with
1> [
1> ArchiveType=cereal::PortableBinaryInputArchive,
1> T=std::shared_ptr<Serializable> &
1> ]
1>main.cpp
1>Generating Code...
1>Done building project "SerializableTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳答案
Afaik,如果你想调用 cereal::base_class<Serializable>(this)
, 那么基类也需要一个 serialize(Archive& ar)
功能。由于没有理由在您的示例中序列化基类(没有成员变量),让我们简化 TestObject
serialize
至 ar(name);
那么你所缺少的就是一个#include <cereal/types/polymorphic.hpp>
或者更好 #include <cereal/types/memory.hpp>
在 Serializable.cpp 中。如 cereal Docs-Polymorphism 中所述, 这包括允许 PortableBinary[Input/Output]Archive
查看 CEREAL_REGISTER_POLYMORPHIC_RELATION
您在衍生品中设置的。
例子:
可序列化.hpp
#pragma once
#include <sstream>
#include <memory>
class Serializable
{
public:
Serializable() = default;
virtual ~Serializable() = default;
virtual bool isAccessible() = 0;
static std::stringstream serialize(const std::shared_ptr<Serializable>& serializable);
static std::shared_ptr<Serializable> deserialize(std::stringstream& serialized);
};
可序列化.cpp
#include "Serializable.hpp"
#include <cereal/archives/portable_binary.hpp>
#include <cereal/types/memory.hpp>
std::stringstream Serializable::serialize(const std::shared_ptr<Serializable>& serializable)
{
std::stringstream ss;
cereal::PortableBinaryOutputArchive ar(ss);
ar(serializable);
return ss;
}
std::shared_ptr<Serializable> Serializable::deserialize(std::stringstream& serialized)
{
cereal::PortableBinaryInputArchive ar(serialized);
std::shared_ptr<Serializable> result;
ar(result);
return result;
}
测试对象.hpp
#pragma once
#include "Serializable.hpp"
#include <string>
#include <cereal/types/polymorphic.hpp>
class TestObject : public Serializable
{
public:
TestObject() = default;
TestObject(const std::string& name) : name(name) {}
~TestObject() = default;
std::string getName() const { return this->name; };
template<class Archive>
void serialize(Archive& ar)
{
ar(name);
};
bool isAccessible() override { return true; };
private:
std::string name;
};
CEREAL_REGISTER_TYPE(TestObject);
CEREAL_REGISTER_POLYMORPHIC_RELATION(Serializable, TestObject)
主要.cpp
#include "TestObject.hpp"
#include "Serializable.hpp"
#include <memory>
#include <sstream>
#include <iostream>
int main(int argc, char **argv)
{
auto test = std::dynamic_pointer_cast<Serializable>(std::make_shared<TestObject>("Test"));
auto ss = Serializable::serialize(test);
auto deserialized = Serializable::deserialize(ss);
auto test2 = std::dynamic_pointer_cast<TestObject>(deserialized);
std::cout << test2->getName();
return 0;
}
输出:
Test
关于c++ - Cereal Polymorphic Serialization 找不到序列化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579809/
“多态”一词从何而来? 最佳答案 它来自希腊词根“poly”(许多)和“morphe”(形式)。多态对象可以采用多种形式(它可以由指向其任何祖先类的指针表示)。多态函数也可以有多种形式(它可以对实际上
我有一个名为 Frame 的基本结构这对一堆计算很有用:。 pub struct Frame { grid_val: Vec, grid_space: Vec, calcula
我有一个名为 Frame 的基本结构这对一堆计算很有用:。 pub struct Frame { grid_val: Vec, grid_space: Vec, calcula
“多态与方法重载或方法覆盖不同。......两者都不是......本身就是多态的实现”。 这是引自 wikipedia 然而,在“面向对象编程”一书中,Timothy Budd 指出有“四种不同形式的
我有一组我没有编写的类,它们是只读的。例如,假设这些类是以下类: public class Base { } public class B : Base { } public class C : B
什么是 Smalltalk 中的“无限动态多态性”?有人可以举个例子吗? 在这个 book 中提到了它:C++ 模板:完整指南,p. 238. 最佳答案 See在 C++ 中:通过继承实现的多态性是有
我在 Laravel 4 中有一个多态关系,它像我想要的那样工作。我有另一个,当我尝试插入相关模型时它不起作用,即使它与第一个相同并且我以相同的方式使用它。 我有 Event模型和 Article模型
我试图弄清楚如何在 Hack 中实现访客模式。它显然需要函数重载多态性,但正如我所测试的,这个例子: visitInt($m); } else if (is_string($m)) {
两者有什么区别? 具有 myMethod(int a) 的父类(super class)以及具有相同方法的继承类, 这是覆盖还是多态? 我很清楚黑白覆盖和重载的区别,但多态性和覆盖似乎是一样的。还是他
来自 Programming Languages: Principles and Paradigms, byMaurizio Gabbrielli, Simone Martini Definition
给定这段代码 locale A = fixes foo :: "'a" locale B = A + fixes bar :: "'a × 'a" locale C' = A + fixe
榆树市可能存在以下情况? func : a -> {a | id : Int} func x = { x | id = 123 } 由于a的多态性太强,因此无法编译。它认为可以是任何东西,包括非记录类
我只想有一个在 Hashtbls 上通用的简单函数,所以我写了这个: let iter_htbl (type a) (module H : Hashtbl.S with type key = a) h
我了解Polymorphic 和Metamorphic 代码的概念,但是最近我同时阅读了两者的Wikipedia页面(出于某种原因,我以前没有这样做过!)。现在,我真的很想为自己编写一些变形代码。 我
多态性是过载的另一个术语吗? 最佳答案 没有;重载是创建一个具有相同名称,带有不同数量参数或具有另一种类型参数的方法。 多态性是指在各种类型(都具有相同的“基本类型”)中更改特定方法的实现/功能。 重
我试图在 OCaml 中表示一组语法的产生式,存在类型对于建模语法规则的语义 Action 非常有用。我一直在研究 Menhir 源代码,存在类型也用于建模语义 Action 。考虑以下因素: typ
我在SML的学习上取得了一些进步,试图巩固我对一些最基本概念的理解。下面的练习证明还有很多东西要学。 我被要求实现一个名为 add 的函数,它接收两个数字并返回它们的总和。问题是,add 应该接收整数
首先,请看这些 Java 代码: Drawable.java package examples.simple.model; public interface Drawable { public
考虑以下代码: trait Animal { fn make_sound(&self) -> String; } struct Cat; impl Animal for Cat { f
考虑以下代码: trait Animal { fn make_sound(&self) -> String; } struct Cat; impl Animal for Cat { f
我是一名优秀的程序员,十分优秀!