在c++中继承vector类时重载方括号
Overloading square brackets while inheriting vector class in c++?
我想从stl vector类继承一个叫做arithmelcevector的类。我的问题是方括号超载。下面是代码:
template<class type>
type& ArithmeticVector<type>::operator[](int index) const{
if(this->size()<=index || index < 0){
throw string("Size Error!");
}else{
return vector<type>::operator[](index);
}
}
给出错误:
将类型为int
的引用绑定到类型为const int
的值时,会删除限定符。
return vector<type>::operator[](index);
我该如何修复它?
您应该删除const
或添加const
:
template <class type>
const type& ArithmeticVector<type>::operator[](int index) const
template <class type>
type& ArithmeticVector<type>::operator[](int index)
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 方括号 [] 运算符重载 C++
- 方括号和圆括号操作器,如何选择重载
- 方括号重载运算符来设置而不是获取 c++
- MyString链表类c++中的方括号运算符[]重载
- 重载多维方括号
- 在c++中继承vector类时重载方括号