- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想将一个带有属性的类序列化为泛型类上的指针列表
这是泛型类派生的父类:
class Base{
public :
friend class boost::serialization::access;
virtual ~Base(){}
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
}
virtual string Getid() = 0 ;
};
通用类:
template<typename T>
class GenericBase : public Base
{
public:
friend class boost::serialization::access;
GenericBase<T>(string id){}
~GenericBase(){}
string id;
vector<T> data
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<Base>(*this);
ar & BOOST_SERIALIZATION_NVP( id);
ar & BOOST_SERIALIZATION_NVP( data);
}
string Getid() { return id; }
};
我要序列化的类
class Use
{
public:
friend class boost::serialization::access;
int Id;
map<string, Base*> BaseDatas;
Use();
~Use();
};
所以,在阅读了 boost 序列化文档 (http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#derivedpointers) 之后,我在序列化代码中尝试了这个:
main(){
Use u = Use();
std::ofstream ofs(filename, ios::binary);
// save data to archive
boost::archive::binary_oarchive oa(ofs);
oa.template register_type<GenericBase<Type1> >();
oa.template register_type<GenericBase<Type2> >();
oa.template register_type<GenericBase<Type3> >();
oa<<u;
}
我收到一条消息,
error: 'template' (as a disambiguator) is only allowed within templates
,所以我替换了
oa.template register_type >();
由
oa.register_type();
它起作用了,我已经能够以文本和二进制格式保存(我检查了数据)
为了现在加载,我只使用了这些行:
main(){
Use u;
std::ifstream ifs(filename, ios::binary);
// load data
ia.register_type<GenericBase<Type1> >();
boost::archive::binary_iarchive ia(ifs);
ia>>u;
}
它给我一个错误:
error: no matching function for call to 'GenericBase::GenericBase()'
有人告诉我,我必须重写 2 个方法保存和加载,就像在这个示例中一样 http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/serialization.html#constructors
namespace boost { namespace serialization {
template<class Archive>
inline void save_construct_data(
Archive & ar, const my_class * t, const unsigned int file_version)
{
// save data required to construct instance
ar << t->m_attribute;
}
template<class Archive>
inline void load_construct_data(
Archive & ar, my_class * t, const unsigned int file_version)
{
// retrieve data from archive required to construct new instance
int attribute;
ar >> attribute;
// invoke inplace constructor to initialize instance of my_class
::new(t)my_class(attribute);
}
}} // namespace ...
但是我必须在哪里定义它们呢?在使用类的声明中?以及我如何与成员(member)打交道
map<string, Base*> BaseDatas;
?
感谢您的帮助;)
最佳答案
如果您提供失败代码的工作(剪切和粘贴)示例以及一些虚拟数据,则评论会更容易......
但我还是试着回答...
Boost.serialisation 试图调用 GenericBases 默认构造函数,但失败了,因为您没有提供它。 Boost.serialisation 首先创建您的对象(或现在尝试),然后读取文件并设置变量。
您可以尝试声明一个 protected 默认构造函数,boost 应该可以通过 access 访问它。
关于C++ Boost 序列化 序列化模板派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3386991/
我有一个无法理解的奇怪编译问题。 //I know, you should never derive from the STL Library template class SharedClass :
我是一个刚开始学习 Haskell 的菜鸟,所以如果我问愚蠢的问题,请耐心等待。 最近我在 SO 中遇到了演示如何导出函数和表达式的类型和实现的问题(诸如 How can I understand "
如何自动派生此 GADT 的 Read 实例: {-# LANGUAGE GADTs, StandaloneDeriving #-} data TypeDec a where TypeDecInt
我遇到了我想要的情况 Deal class要注意它DealDetail type反之亦然,我想 DealDetail注意Deal type .将来我想有很多 Deal 的后代和 DealDetails
我是 C# 新手,所以请多多包涵。 好的,所以我在不同的程序集中有两个类需要相互引用: namespace AssemblyA { class A { private B MyB {
简而言之,我已经实现了一个派生自 SynchronizationContext 的类,以便 GUI 应用程序可以轻松地使用在 GUI 线程以外的线程上引发的事件。我非常感谢对我的实现的评论。具体来说,
我正在设计一个小型系统,想知道如何为派生类分配内存的细微差别。 如果我有两个类(class) class foo { public: int a; Foo(): a(0) {}; }; class
我正在尝试编写一个派生 PartialEq 的枚举,其中包含一个手动执行此操作的特征对象。我使用了解决方案 here为了强制 Trait 的实现者编写相等方法。这无法编译: trait Trait {
以下代码可以编译(特别是 MyError 被识别为具有调试特性): use std::str; use std::fmt; #[derive(Debug)] enum MyError where F:
是否有一种简单的方法来注释结构中的字段,以便在派生 PartialEq 特征时忽略它们?例如: #[derive(PartialEq,Eq)] pub struct UndirectedGraph {
我正在编写代码来处理“Foo”类型的对象。 foo 是一种容器,为了提供对其元素的高效和抽象访问,它提供了 Element 类型的嵌套类。 Element 包装对象在容器中的位置。 现在,“Foo”可
假设如下: class child : public parent { public: fun1(parent * obj); //somewhere on the child class
我有几个模板类 template class Transition { public: virtual Cost getCost() = 0; }; template class St
我正在尝试使用自定义 QSortFilterProxyModel . 这是我的标题: #include class QSortFilterProxyModel_NumbersLast : publi
我正在使用 C# 和 mvc3。我在解决方案中添加了一个项目。我想创建一个新 Controller 并让它从我添加的项目中的 Controller 派生。我该怎么做? 最佳答案 在 Visual St
我在 python 中有一个对象,它派生自 QtGui.QGraphicsPixmapItem,具有一些基本属性和方法。在对此对象的引用上调用 deepcopy 后,当我尝试使用该副本时收到一条错误消
由于只能给FixedDocument添加页面,所以我写了一个派生类: public class CustomFixedDocument : FixedDocument { public voi
我在自定义 QMainWindow 时遇到了很大的问题,因为我不知道如何实现以下内容: 在 QMainWindow 文档中,QMainWindow 有一些用于工具栏、停靠小部件、状态栏和其他的特殊区域
我想感受一下QT,决定写一个小的十六进制编辑器。为此,我需要一个允许滚动的小部件。经过一番研究,我发现 QTextEdit 为此目的派生自 QAbstractScrollArea。在阅读 QAbstr
我正在寻找一种可以从已经发生的洗牌过程中派生出 key 的算法。 假设我们有被打乱的字符串“Hello”: "hello" -> "loelh" 现在我想从中导出一个 key k,我可以用它来撤销洗牌
我是一名优秀的程序员,十分优秀!