Boost::icl::no_type error

Boost::icl::no_type error

本文关键字:type error no icl Boost      更新时间:2023-10-16

我在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,…)

有人能帮忙吗?

下面是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);
}

在文件中找到:boosticltype_traitsdifference_type_of.hpp

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

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

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

虽然,你的类型看起来像一个数字值的waper,也许boost头文件没有得到它。请在您的头文件中测试此代码片段,不要使用专有的命名空间。

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

如果它不能正常工作,诀窍是告诉boost你的类型有一个不同的类型(CDouble),这样复制构造函数就能正常工作。

谢谢你的回答,但我用这个代替:

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