为什么是“没有可行的操作员过载”
Why is 'No viable operator overload'?
我正在编写一个 2D 数组类并尝试重载运算符[]:
typedef unsigned long long int dim;
template<typename N>
class Array2D {
private:
dim _n_rows;
dim _n_cols;
vector<vector<N>> M;
public:
dim n_rows() { return _n_rows; }
dim n_cols() { return _n_cols; }
Array2D(): _n_rows(0), _n_cols(0), M(0, vector<N>(0)){}
Array2D (const dim &r, const dim &c) : _n_rows(r), _n_cols(c), M(r, vector<N>(c)) {}
void set(const dim &i, const dim &j, const N &elem) { M[i][j] = elem; } // Works fine
vector<N>& operator[](int &index) { return M[index]; } // <- PROBLEM
};
我看到它的方式:运算符 [] 返回一些东西(向量),而它又具有重载的运算符 []。这就是为什么我认为
Array2D<int> L(10, 10);
L[3][3] = 10;
应该工作。
显然,编译器不这么认为,说"对于类型'Array2D'没有可行的重载运算符[]"我做错了什么以及如何解决它?
附言XCode 7,如果这很重要的话。
此函数:
vector<N>& operator[](int &index)
不能这样称呼:
Array2D<int> L(10, 10);
L[3][3] = 10;
因为不能采用对文字的非常量引用。 引用允许修改,修改3
意味着什么?
您应该改用它:
vector<N>& operator[](size_t index)
相关文章:
- 为什么"delete"操作员给我访问权限冲突
- 为什么使操作员成为新的专用会打破 std::shared_ptr?
- 为什么新操作员挂起我的程序?
- 为什么我不能拥有纯虚拟分配操作员?
- 为什么STD :: SPAN超载函数调用操作员索引
- 为什么只有一行告诉我操作员=不可行?但是其他人很好
- 为什么教科书使用初始化列表作为操作员的返回值(复杂A)
- 为什么我们需要宇宙飞船<=>操作员C++?
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用
- 为什么在C 中将任意数量的提前/减少操作员放在一起
- 为什么继承的受保护操作员=()有公共访问权限
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 为什么要调用复制操作员
- 为什么我的作业操作员给出了损坏错误
- 为什么STD :: SLICE_ARRAY ::操作员= const
- 为什么我超负荷的铸造操作员无法访问私人成员
- 为什么逗号运算符在运算符[]中打电话给操作员[],而不是在Operator()内部调用
- 为什么返回Sfinae转换操作员不起作用
- 为什么C 标准要求编译器忽略对基本类型转换操作员的呼叫