在哪里声明BOOST_CLASS_EXPORT进行序列化 [英] Where to declare BOOST_CLASS_EXPORT for serialization

查看:179
本文介绍了在哪里声明BOOST_CLASS_EXPORT进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要序列化数据,并且具有多态类.

I want to serialize my data and i have polymorphic classes .

最初,当我学习序列化的过程时,我在其头文件中声明了Inherited类.

Initially when i was learning the process of the Serialization i declared the Inherited class in their header files.

  Header file for Sphere which is sub class of geometry
  BOOST_CLASS_EXPORT(Sphere)

但是当我尝试在我的项目中执行操作时,出现链接器错误2005,指出该对象已在对象中定义.

but when i tried doing in my project i got the linker error2005 stating that the object is already defined in the object.

因此,我尝试在cpp文件中声明BOOST_CLASS_EXPORT(Sphere),但随后我得到了未注册或导出错误消息的派生类.

So i tried declaring the BOOST_CLASS_EXPORT(Sphere) in the cpp file but than i got error message derived class not registered or exported.

最终,我通过在Main文件中声明派生类使它起作用.

Eventually i got it working by declaring the derived class in the Main file.

  BOOST_CLASS_EXPORT_GUID(Sphere, "Sphere"); 

但是这里的问题是,如果我有30个对象,则需要在主文件中声明它们;如果我添加任何新类,则需要再次在主文件中声明它们.

But the problem here is that if i have 30 objects i will need to declare them in the main file and if i add any new classes than again i need to declare them in main file.

我可以在其类的标头或cpp文件中定义导出.

Can i define the export in their header or cpp files of the class.

推荐答案

您要分隔导出KEY和IMPLEMENTATION.

You want to separate the export KEY and IMPLEMENTATION.

我有一些非常完整的答案,其中一些还涉及多态性和抽象基础.看到那里有很好的建议:

I have some very complete answers up, some of which also touch on polymorphism and abstract bases. See there for good suggestions:

  • The simplest introduction Boost Serialization - Exporting in multiple CPP files (no code, but perhaps most explanatory)
  • A complete example with more explanation and documentation quotes, in multiple translation units: Boost serialization, loading an archived class by base type gives wrong data
  • BOOST_CLASS_EXPORT_* macros are not working like register_type() (complete with several source files and makefile)
  • Another sample with multiple translation units boost serialization of nested struct does not work
  • A subtle pitfall mixing static polymorphism and dynamic polymorphism: Serialize Polymorph Interface
  • Why does boost::serialize not work despite everything seeming right? ("unregistered class")
  • Rocket science hacks: Boost serialization of derived class with private members

这篇关于在哪里声明BOOST_CLASS_EXPORT进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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