c++ - 从成员函数返回 boost 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
这里可能有什么问题?在此先感谢您的帮助?
解决方案
常数是问题。
你的range
方法是const
。
&container[0]
insideconst
方法的类型是什么?它是const double*
。它不匹配
boost::make_iterator_range< double* >
^^^^^^^^
所以将range
成员函数定义为非常量或使用boost::make_iterator_range< const double*>
。
推荐阅读
- .net - .NET 调试器:不加载所有符号
- android - 将文件保存在内部存储目录中
- oracle-apex - 动态设置 Oracle Apex 5.1 交互式网格区域标题
- visual-studio - VS2019 - Android - Android.Content.Res.Resources+NotFoundException:
- python - 将 1d numpy 数组重塑为 2d
- c - 如何将整数转换为char数组[]?
- java - 无法实例化由 InvocationTargetException 和 NullPointerException 引起的类
- javascript - node.js的导入导出
- c# - 从 ListView C# 中选择第二个项目时出错
- sql - 是否可以通过加入提出此请求?