简体   繁体   English

从成员函数返回boost iterator_range

[英]returning boost iterator_range from a member function

I'm trying to create a member function that returns range of an array like below: 我正在尝试创建一个成员函数,该函数返回如下数组的范围:

#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;
}

But It gives below errors: 但是它给出了以下错误:

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    

What might be the problem here? 这可能是什么问题? Thanks in advance for your help? 在此先感谢您的帮助?

constness is problem. 常数是个问题。

Your range method is const . 您的range方法是const

What is type of &container[0] inside const method? const方法中的&container[0]是什么类型? It is const double* . 它是const double* It doesn't match to 与...不符

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

So define range member function as non-const or use boost::make_iterator_range< const double*> . 因此,将range成员函数定义为非const或使用boost::make_iterator_range< const double*>

It worked when I changed it like below: 当我如下更改时,它起作用了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM