【问题标题】:Boost::icl::no_type errorBoost::icl::no_type 错误
【发布时间】:2013-01-07 15:29:49
【问题描述】:

我在 Visual Studio 2008 中收到此错误: 错误 1 ​​错误 C2664: 'BaseUtil::Type::CDouble::CDouble(const BaseUtil::Type::CDouble &)' : 无法将参数 1 从 'boost::icl::no_type' 转换为 'const BaseUtil::Type ::CDouble &'

这里是我的类界面:

class CDouble
{
public: 
  CDouble();
  CDouble(const CDouble& _obj);
  CDouble(const double& _val);

  bool operator==(const CDouble& _obj) const;
  bool operator==(const double& _obj) const; 
  bool operator!=(const CDouble& _obj) const;
  bool operator<=(const CDouble& _obj) const;
  bool operator>=(const CDouble& _obj) const;
  bool operator< (const CDouble& _obj) const;
  bool operator> (const CDouble& _obj) const;

  CDouble& operator= (const CDouble& _obj);
  CDouble& operator+=(const CDouble& _obj);
  CDouble& operator-=(const CDouble& _obj);

  const CDouble operator+(const CDouble& _obj) const;
  const CDouble operator-(const CDouble& _obj) const;

  const double operator/(const CDouble& _obj) const;

  CDouble& operator= (double _value);
  CDouble& operator+=(double _value);
  CDouble& operator-=(double _value);
  CDouble& operator*=(double _value);
  CDouble& operator/=(double _value);

  const CDouble operator+(double _value) const;
  const CDouble operator-(double _value) const;
  const CDouble operator*(double _value) const;
  const CDouble operator/(double _value) const;

  operator double() const {return m_value;} 

private:
  CDouble& operator*=(const CDouble&  _obj);
  const CDouble operator*(const CDouble&  _obj) const;
  CDouble& operator/=(const CDouble&  _obj);

  double m_value;
};

触发编译错误的代码:

  template <class BoundType>
  class Interval
  {
  public:
    BoundType Length() const
    {
      return boost::icl::length(
        boost::icl::construct<boost::icl::interval<BoundType>::type>(m_LowerBound,    m_UpperBound, m_IntervalType())
       );
    }

  private:
    BoundType m_LowerBound, m_UpperBound; 
    typedef boost::icl::interval_bounds (*IntervalType)(); 
    IntervalType m_IntervalType;
  }

  int main()
  {
    Interval<CDouble> typeDouble(-1.0, 1.0);
    typeDouble.Length(); //<-- COMPILE ERROR
  }

我不明白错误也不知道如何解决。

它适用于基本类型(int,double,..)

有人可以帮忙吗?

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    这是来自 boost 1.52 头文件的长度函数:

    template<class Type>
    inline typename boost::enable_if<is_continuous_interval<Type>, 
      typename difference_type_of<interval_traits<Type> >::type>::type
    length(const Type& object)
    {
        typedef typename difference_type_of<interval_traits<Type> >::type DiffT;
        return icl::is_empty(object) ? identity_element<DiffT>::value()
                                     : upper(object) - lower(object);
    }
    

    在文件中找到:boost\icl\type_traits\difference_type_of.hpp

    template <class Type>
    struct get_difference_type<Type, false, false>
    {
        typedef no_type type;
    };
    

    所以我假设支持差分数值运算符的类型的 boost 头文件默认实现是 no_type

    必须做的是,在编译时提供与您的构造函数之一匹配的差异类型的定义。即,例如,构造函数副本就是您的情况。

    虽然,您的类型看起来像是数值上的 wapper,但也许 boost 头文件没有得到它。请在您的一个头文件中测试这个 sn-p,在专有名称空间之外。

    #include <boost_1_52_0\boost\icl\type_traits\is_numeric.hpp>
    
    namespace boost{ namespace icl
    {
        template <> 
        struct is_numeric<CDouble>
        {
            typedef is_numeric type;
            BOOST_STATIC_CONSTANT(bool, value = true );
        };
    } }
    

    如果它不能按原样工作,诀窍是告诉 boost 你的类型具有差异类型(CDouble),以便复制构造函数起作用。

    【讨论】:

      【解决方案2】:

      感谢您的回答,但我改用了这个:

      namespace std
      {
        template <> 
        class numeric_limits<BaseUtil::Type::CDouble> : public numeric_limits<double>
        {
        };
      }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-20
        • 1970-01-01
        • 2015-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多