c++ - 从成员函数返回 boost iterator_range

标签 c++ boost iterator iterator-range

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

#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    

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

最佳答案

constness 是个问题。

你的 range方法是 const .

什么是 &container[0] 的类型里面const方法?是const double* .它不匹配

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

所以定义range成员函数作为非常量或使用 boost::make_iterator_range< const double*> .

关于c++ - 从成员函数返回 boost iterator_range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54486646/

相关文章:

Java多线程和迭代器,应该很简单,初学者

c++ - 动态和静态内存分配?

c++ - 使用 aqtime 测量单元测试的代码覆盖率

c++ - boost lambda 的使用

c++ - Eclipse for C++ 无法识别酿造的 GSL 和 Boost 库

iterator - 使用 `@transform` 在 Julia 中转换 DataFrame

debugging - Rust:如何在闭包参数中指定生命周期?

c++ - 将 boost::regex_search() 与字符串迭代器一起使用

c++ - 如何更恰本地处理好友功能?

java - 在C++中模拟Java的Thread类