【问题标题】:assertion_failed when using Boost Serialization with xml_oarchive使用带有 xml_oarchive 的 Boost 序列化时 assertion_failed
【发布时间】:2014-02-02 07:46:19
【问题描述】:

编译Boost序列化的简单测试时:

class Test
{
protected:
    int Num;

    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(Num);
    }

public:
    Test(): Num(0) {}
    ~Test() {}
};

使用 xml_oarchive 进行输出,我遇到以下 GCC 错误:

C:\Development\Libraries\boost_1_55_0\boost\mpl\assert.hpp|289|error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<Test>::************)'|

当使用其他存档类型时,它编译并运行良好。我所做的所有研究都指向我使用BOOST_SERIALIZATION_NVP 宏来解决错误,但我已经这样做了,我仍然得到这个错误。

有没有人遇到过同样的问题?

【问题讨论】:

  • 旁注:BOOST_SERIALIZATION_NVP 是一个围绕 boost::serialization::make_nvp(const char * name, T & t) 的简单宏。您可能不会公开成员名称,而是使用 make_nvp("Number", Num)

标签: c++ boost boost-serialization


【解决方案1】:

如boost序列化源码中所述:

// Anything not an attribute and not a name-value pair is an
// error and should be trapped here.
template<class T>
void save_override(T & t, BOOST_PFTO int)
{
    // If your program fails to compile here, its most likely due to
    // not specifying an nvp wrapper around the variable to
    // be serialized.
    BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
    this->detail_common_oarchive::save_override(t, 0);
}

您可能不会将它与您的 Test 对象一起使用:

Test test;
ar << BOOST_SERIALIZATION_NVP(test);

【讨论】:

  • 哦,该死的,我错过了那部分。我没有意识到我还必须对整个对象使用BOOST_SERIALIZATION_NVP,但这确实是有道理的。谢谢!
  • 旁注:我确实尝试遍历 Boost 源代码以查找断言失败的位置,但我做不到。我的编译器错误将我指向一堆 #defines 和宏魔法。
【解决方案2】:

当我忘记在新项目中包含定义它的源时,我得到 boost::assertion_failed undefined 错误。

独立于其他任何内容,您应该定义 boost::assertion_failed,如 http://www.boost.org/doc/libs/1_55_0/libs/utility/assert.html(与 OP 的过时问题匹配的版本,但在最新版本中类似)和 https://stackoverflow.com/a/21508521/527489 的答案中所述

如文档中所述,“用户应提供适当的定义。”

定义这可能会使编译器错误不那么令人沮丧(尽管可能仍然非常令人沮丧)。

【讨论】:

    【解决方案3】:

    我想补充一点,Boost.Serialization 的文档实际上明确说明了这一点,因为似乎没有人提到它。

    您的代码仅在使用 xml_oarchive 而不是其他 oarchive 类型时编译失败。这是boost的官方解释。

    来自http://www.boost.org/doc/libs/1_45_0/libs/serialization/doc/wrappers.html#nvp
    (强调我的)

    XML 档案有一种特殊的情况。 XML 格式有一个嵌套 很好地映射到“递归类成员访问者”的结构 序列化系统使用的模式。但是,XML 不同于 其他格式它需要每个类数据成员的名称。 我们的目标是将此信息添加到类序列化中 规范,同时仍然允许序列化代码是 与任何存档一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2012-12-30
      • 1970-01-01
      • 2013-07-15
      • 2015-06-16
      • 2012-08-12
      • 1970-01-01
      相关资源
      最近更新 更多