【问题标题】:returning boost iterator_range from a member function从成员函数返回 boost iterator_range
【发布时间】:2019-06-26 10:51:42
【问题描述】:

我正在尝试创建一个返回数组范围的成员函数,如下所示:

#include <boost/range/iterator_range.hpp>

class MyClass {
public:
    boost::iterator_range< double* > range() const{ 
    boost::iterator_range< double* > itr_range = boost::make_iterator_range< double* >(&container[0], &container[m_size]);
    return itr_range;
 }
private:
    double container[4] {0.0, 1.0, 2.0, 3.0}; 
    size_t m_size = 4;
};

int main() {
   MyClass obj;   

   return 0;
}

但它给出了以下错误:

no matching function for call to 'make_iterator_range(const double*, const double*)' main.cpp line 6

'double*' is not a class, struct, or union type range_test      line 37, external location: /usr/include/boost/range/const_iterator.hpp 

'double*' is not a class, struct, or union type range_test      line 37, external location: /usr/include/boost/range/mutable_iterator.hpp   

required by substitution of 'template<class Range> boost::iterator_range<typename boost::range_iterator<C>::type> boost::make_iterator_range(Range&, typename boost::range_difference<Left>::type, typename boost::range_difference<Left>::type) [with Range = double*]'    range_test      line 616, external location: /usr/include/boost/range/iterator_range.hpp


 required by substitution of 'template<class Range> boost::iterator_range<typename boost::range_iterator<const T>::type> boost::make_iterator_range(const Range&, typename boost::range_difference<Left>::type, typename boost::range_difference<Left>::type) [with Range = double*]'   range_test      line 626, external location: /usr/include/boost/range/iterator_range.hpp    

这里可能有什么问题?提前感谢您的帮助?

【问题讨论】:

    标签: c++ boost iterator iterator-range


    【解决方案1】:

    当我像下面这样更改它时它起作用了:

    boost::iterator_range<const double*> range() const{ 
        return boost::make_iterator_range(&container[0], &container[m_size]);
    }
    

    【讨论】:

      【解决方案2】:

      常量是个问题。

      您的range 方法是const

      const 方法中的&amp;container[0] 是什么类型?它是const double*。不匹配

      boost::make_iterator_range< double* >
                                  ^^^^^^^^
      

      因此将range 成员函数定义为非常量或使用boost::make_iterator_range&lt; const double*&gt;

      【讨论】:

        猜你喜欢
        • 2010-12-15
        • 2021-12-08
        • 2023-04-09
        • 2012-06-24
        • 2012-06-18
        • 1970-01-01
        • 2018-05-22
        • 2012-10-29
        • 2012-10-28
        相关资源
        最近更新 更多