我在编写代码时遇到问题:
void main(){
Matrix c(rows,cols);//rows & cols are int numbers
c[0][0]=2//the line that I'm having a problem to do the operator
}
//My class defined like this:
class Matrix{
public:
Matrix(int rows,int cols): rows(rows), cols(cols){
mat= new double*[cols];
for( int i=0; i<rows;i++){
*mat=new double[i];
}
}
private:
int rows,cols;
double **mat;
};
我怎样才能创建一个操作员来帮助我完成我遇到问题的线路?
没有operator [][]
, but operator[]
。这样就应该返回一些你可以使用的东西[]
也是(指针或代理类)。
对于您的情况,您可以简单地执行以下操作:
double* operator[](int i) { return mat[i]; }
const double* operator[](int i) const { return mat[i]; }
对于更复杂的情况,您必须返回一个代理类。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)