如何使用 boost 序列化 3D 数组?
好吧,我最近切换到 boost,并且我部分理解了序列化简单对象或简单类(boost 教程让我感到困惑),但是如何序列化包含类成员的 3D 数组?
我有一个名为 TileList 的数组 (std::vector),其中包含属于 Tile 类的对象,而 Tile 类只包含两个变量:int TileID 和 int TilePassability。
我尝试像序列化教程中的非侵入式方法和 STL 容器方法那样进行操作,但我只是得到一堆错误作为回报。有人可以解释如何正确地做到这一点吗?谢谢。
Okay, I recently switched to boost and I partitialy understand serializing simple objects or simple classes (the boost tutorial confuses me), but how can I serialize a 3D array which contains class members?
I have an array (std::vector) called TileList, which contains objects which are of a class Tile and class Tile contains nothing else than two variables, int TileID and int TilePassability.
I tried doing it as the Serialization tutorial does in the non obtrusive method and the STL Container method, but I just get a stack of errors in return. Can somebody explain how to do it properly? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我知道你有这样的东西(这不是真正的 3D 数组,如果我误读了你,请发布你想要序列化的确切代码):
因为
std::vector
已经可以通过标准 Boost.Serialization 序列化设施,你只需要使Tile
可序列化。最简单的方法是添加一个执行加载和保存操作的serialize
成员。就这样,您的类型现在可以序列化了。需要考虑的特殊事项包括类成员是私有的——在这种情况下,您需要为 Boost.Serialization 添加友元声明,即
serialize
成员必须是一个模板(好吧,不是真的;但直到您知道库更好一点,它必须是),因此它的整个主体必须包含在类声明中(除非您使用显式实例化,但这也不适合初学者)。您还可以将其拆分为
save
和load
成员,但这在您开始使用版本控制之前没有用处。它看起来像这样:这使得向量所持有的类型可序列化。序列化向量本身只是
archive &瓦片列表
。So I understand you have something like this (that's not really a 3D array, post the exact code you want to serialise if I misread you):
Because
std::vector
is already serialisable through standard Boost.Serialization facilities, you only need to makeTile
serialisable. The easiest way is to add aserialize
member that does both the loading and saving.Aand that's about it, your type is now serialisable. Special things to be considered include class members being private — you need to add friend declaration for Boost.Serialization in this case, i.e.
serialize
member have to be a template (well, not really; but until you know the library a bit better, it have to be), so its entire body must be included within the class declaration (unless you use explicit instantiation, but that's also not something for beginners).You can also split it into
save
andload
members, but that's not useful until you start using versioning. It would look like this:That makes the type held by the vector serialisable. Serialising a vector itself is just
archive & tileList
.