1

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?

zontragon
  • 780
  • 1
  • 9
  • 27

2 Answers2

2

constness is problem.

Your range method is const.

What is type of &container[0] inside const method? It is 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*>.

rafix07
  • 20,001
  • 3
  • 20
  • 33
0

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]);
}
zontragon
  • 780
  • 1
  • 9
  • 27