如何使用 Boost 序列化序列化 Boostscoped_array?

发布于 2024-11-26 14:26:15 字数 247 浏览 3 评论 0原文

我正在尝试使用 Boost 序列化来序列化 Boost scoped_array,但编译器 (VS2008) 给出以下错误消息:

error C2039: 'serialize' : is not a member of 'boost::scoped_array<T>'

如何序列化 scoped_array?我应该为此添加一个 Boost 库吗?

I am trying to serialize a Boost scoped_array using Boost serialization but the compiler (VS2008) is giving me the following error message:

error C2039: 'serialize' : is not a member of 'boost::scoped_array<T>'

How do I serialize a scoped_array? Is there a Boost library that I should be including for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花细雨 2024-12-03 14:26:15

猜猜不是。 scoped_ptrscoped_array 旨在跟踪本地范围内的指针。

scoped_ptr 模板是满足简单需求的简单解决方案。它提供了基本的“资源获取即初始化”设施,没有共享所有权或所有权转让语义。它的名称和语义的执行(通过不可复制)都表明它仅在当前范围内保留所有权的意图。

将内容序列化并稍后读回似乎违背了课程的意图。

Guess not. The scoped_ptr and scoped_array are designed for keeping track of pointers in a local scope.

The scoped_ptr template is a simple solution for simple needs. It supplies a basic "resource acquisition is initialization" facility, without shared-ownership or transfer-of-ownership semantics. Both its name and enforcement of semantics (by being noncopyable) signal its intent to retain ownership solely within the current scope.

Having the content serialized and read back later seems to be against the intent of the class.

长伴 2024-12-03 14:26:15

序列化数组本身,而不是它周围的内存管理包装器。

Serialise the array itself, not a memory-managing wrapper around it.

强者自强 2024-12-03 14:26:15

这是我最终使用的解决方案(对称——适用于保存和加载):

void myClass::serialize(Archive & ar, const unsigned int file_version)
{
    ar & myScopedArraySIZE;

    // Only gets called during loading
    if (Archive::is_loading::value)
    {
        myScopedArray.reset(new ColourPtr[myScopedArraySIZE]);
    }

    for (uint i = 0; i < myScopedArraySize; i++)
    {
        ar & myScopedArray[i];
    }
}

Here is a solution that I ended up using (symmetric -- works for saving and loading):

void myClass::serialize(Archive & ar, const unsigned int file_version)
{
    ar & myScopedArraySIZE;

    // Only gets called during loading
    if (Archive::is_loading::value)
    {
        myScopedArray.reset(new ColourPtr[myScopedArraySIZE]);
    }

    for (uint i = 0; i < myScopedArraySize; i++)
    {
        ar & myScopedArray[i];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文