Class operators

前端 未结 2 1251
日久生厌
日久生厌 2021-01-29 14:41

I\'m having a problem to make the code:

void main(){
     Matrix c(rows,cols);//rows & cols are int numbers
     c[0][0]=2//the line that I\'m having a probl         


        
2条回答
  •  醉梦人生
    2021-01-29 15:18

    There are no operator [][], but operator[]. So that one should return something for which you can use [] too (pointer or proxy class).

    In your case, you might simply do:

    double* operator[](int i) { return mat[i]; }
    const double* operator[](int i) const { return mat[i]; }
    

    For more complicated cases, you have to return a proxy class.

提交回复
热议问题