【问题标题】:Boost de-serialized Object has nan or -nan valuesBoost 反序列化对象具有 nan 或 -nan 值
【发布时间】:2020-10-24 23:25:41
【问题描述】:

我最近询问了关于反序列化地图的问题,我在这里得到了答案:De-Serializing an STL map class member 我现在面临的问题是我得到了一些Point 对象值的nan 值。这是一个示例序列化字符串:

22 serialization::archive 16 0 0 47 uploads/guest/another_project/90.step_1/90.step 0 0 1 0 0 0 1 0 0 0 0 1 9.00000000000000000e+01 1 0 0 -2.400000000000000000000e+01 1.000000000000000000000e+00 -2.500000000000000000000e+01 -2.400000000000000000000e+01 1.000000000000000000000e+00 2.500000000000000000000e+01

我决定在序列化和反序列化期间打印值,并显示正确的值。

...
class Point
{
  friend class boost::serialization::access;
  friend std::ostream & operator<<(std::ostream &os, const Point &p);

  template<class Archive>
  void serialize(Archive &ar, const unsigned int version)
  {
    // save/load base class information
    ar & X & Y & Z;

    std::cout << "(" << X << ", " << Y << ", " << Z << ")" << std::endl; // OK
  }
...
};

Point 对象的操作返回 inf/nan/不正确的结果,所以我决定在另一个函数中打印 Point 对象,而不是获取 Point 值,例如 (-24, 1, -25) &amp; (-24, 1, 25) 我得到的是 (-nan, -nan, -nan) &amp; (-nan, -nan, nan)

我在Bend 类中有 2 个 Point 对象 startp 和 endp,它们的序列化如下:

template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
    // save/load base class information
     ar & boost::serialization::base_object<MFace>(*this);
     ar & mBendAngle & mBendDirection & startp & endp;
}

我必须使用 2 个Point 对象创建另一个名为弯线的对象:这是我打印点的函数

  void ModelBend::makeBendLine(){
    gp_Pnt endPoint(
      startp.X,
      startp.Y,
      startp.Z
    );

    gp_Pnt dirVertex(
      startp.X - endp.X,
      startp.Y - endp.Y,
      startp.Z - endp.Z
    );

    bendLine_ = gp_Lin(endPoint, gp_Dir(dirVertex.X(), dirVertex.Y(), dirVertex.Z()));
    
    std::cout << "==========================================="  << std::endl;
    std::cout << "(" << startp.X << ", " << startp.Y << ", " << startp.Z << ")" << " ==== ";
    std::cout << "(" << endp.X << ", " << endp.Y << ", " << endp.Z << ")" << std::endl;
    std::cout << "==========================================="  << std::endl;
  }

此函数从 ModelBend 构造函数中调用。这是我获得 SOME 点值的 nan 值的地方。

【问题讨论】:

  • 所以您从serialize 打印的内容表明您没有从序列化/反序列化中得到 nan,但您的问题标题表明您是。我有点困惑。肯定有证据表明您的程序中的其他地方存在错误。
  • @john 我不确定如何呈现它。现在怎么样了?
  • 我的观点是没有证据表明序列化或反序列化是问题所在(事实上证据恰恰相反)。根据您在此处提供的内容,您的代码中的其他地方似乎存在错误。但如果无法查看您的其余代码,就无法提供帮助。

标签: c++ boost boost-serialization


【解决方案1】:

我更改了代码并从序列化函数调用 makeBendLine(),而不是在构造函数 ModelBend() 中调用,如下所示,现在一切正常:

template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
    // save/load base class information
     ar & boost::serialization::base_object<MFace>(*this);
     ar & mBendAngle & mBendDirection & startp & endp;

     makeBendLine();  // LIKE THIS
}

【讨论】:

    【解决方案2】:

    序列化的时候需要执行函数吗?

    可能不会。 为此,您具有 is_saving/is_loading 特征。

    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        // save/load base class information
         ar & boost::serialization::base_object<MFace>(*this);
         ar & mBendAngle & mBendDirection & startp & endp;
    
         if(Archive::is_loading::value) makeBendLine();
    }
    

    (真正的问题是 makeBendLine 可能会触及内存以进行名义上为 const 的保存操作。)

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 2017-09-24
      • 2013-08-28
      相关资源
      最近更新 更多