尽管看起来一切正常,为什么boost :: serialize无法正常工作? (“未注册的课程") [英] Why does boost::serialize not work despite everything seeming right? ("unregistered class")

查看:175
本文介绍了尽管看起来一切正常,为什么boost :: serialize无法正常工作? (“未注册的课程")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很疑惑.我有一个C ++程序,其中包含许多数据结构,这些数据结构是从公共根导出的,我需要使用Boost对其进行序列化.每个人都有一个内联成员函数来接受访问者(因此我可以访问结构而无需使用"switch"语句).

I am wondering about this. I have a C++ program with a number of data structs which derive from a common root and I need to serialize them using Boost. Each one has an inline member function to accept a visitor (so I can visit the structure without a "switch" statement).

对象看起来像这样:

在.h文件中:

// Graphic component.
struct GraphicComponent : public Component {
  ... data members ...
  void accept(ComponentVisitor &vis) { vis.visitGraphicComponent(*this); }

 private:
  // Serialization routine.
  friend class boost::serialization::access;

template<class Archive>
  void serialize(Archive &a, const unsigned int v);
};
BOOST_CLASS_EXPORT_KEY(GraphicComponent)

// Position component.
struct PositionComponent : public Component {
  ... data members ...
  void accept(ComponentVisitor &vis) { vis.visitPositionComponent(*this); }

 private:
  // Serialization routine.
  friend class boost::serialization::access;

template<class Archive>
  void serialize(Archive &a, const unsigned int v);
};
BOOST_CLASS_EXPORT_KEY(PositionComponent)

...

在.cpp文件中,我声明了序列化"例程:

In the .cpp file, I declare the "serialize" routines:

BOOST_CLASS_EXPORT_IMPLEMENT(GraphicComponent)
BOOST_CLASS_EXPORT_IMPLEMENT(PositionComponent)
...

template<class Archive>
  void GraphicComponent::serialize(Archive &a, const unsigned int v)
  {
    a & boost::serialization::base_object<Component>(*this);
    ... serialize data members ...
  }

template<class Archive>
  void PositionComponent::serialize(Archive &a, const unsigned int v)
  {
    a & boost::serialization::base_object<Component>(*this);
    ... serialize data members ...
  }

...

我还通过通用标头包含Boost存档.据我所知,一切看起来都不错.基本组件上还有一个"BOOST_SERIALIZATION_ASSUME_ABSTRACT",因为"accept"是纯虚拟的.

I also include the Boost archive through a common header. As far as I can tell, everything looks right. There's also a "BOOST_SERIALIZATION_ASSUME_ABSTRACT" on the base Component, as "accept" is pure virtual.

当我运行程序并到达将其序列化的位置时,我得到了

When I run the program and get to the point where it serializes this stuff, I get

 what():  unregistered class - derived class not registered or exported

串行化是通过指向基本Component的指针进行的.

Serialization occurs through a pointer to the base Component.

我听说过涉及Boost序列化和库"的麻烦.我使用的构建系统CMake通过将其子组件组装到库中,然后将它们的子组件放到一个可执行文件中来构成最终程序,从而对程序进行编译.可能是问题所在吗?

I've heard troubles involving Boost serialization and "libraries". The build system I was using, CMake, is set up to compile the program by assembling its subcomponents into libraries and then putting those together into a single executable to make the final program. Could that be the problem?

此外,组件从std :: enable_shared_from_this派生(这是C ++ 11 STL,而不是Boost)-这可能是问题吗?如果是这样,该怎么办?

Also, Component derives from std::enable_shared_from_this (that's C++11 STL, not Boost) -- could this be the problem? If so, what can be done about it?

推荐答案

这是部分答案,因为它无法确切说明失败的原因.我设法通过将程序编译为单个程序,而不是将一堆库静态地链接在一起,从而解决了这个问题,这就是我认为我必须对使用的构建系统进行处理的方式,因为该系统的在线可用非常简洁,当我将makefile放在一起时,我不确定到底该怎么做.我怀疑这与Boost处理库中此类代码的麻烦有关.

This is a partial answer as it doesn't explain exactly why it failed. I have managed to solve the problem by compiling the program as a single program instead of a bunch of libraries that are then statically linked together, which is how I thought I had to do it with the build system I was using since the documentation that was available online for the system was terse and when I put together the makefiles, I wasn't sure exactly how to do it. I suspect it has something to do with Boost's trouble dealing with this kind of code in libraries.

这篇关于尽管看起来一切正常,为什么boost :: serialize无法正常工作? (“未注册的课程")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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