【发布时间】:2018-12-28 00:13:16
【问题描述】:
我在编写代码时遇到问题:
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;
};
我怎样才能创建一个操作员来帮助我完成我遇到问题的生产线?
【问题讨论】:
-
您应该注意
void main不是有效的 C++。只有int main,所以最好使用它,因为a)这是C++规则规定的,b)无论如何写起来更短 -
@MooingDuck 它在构造函数体中。短语“方法”在 C++ 中没有特殊含义,但我认为在通俗意义上我们可以将构造函数等特殊成员函数视为“方法”。
-
您可以使用
double& Matrix::operator()(int r, int c);方式访问矩阵中的元素。
标签: c++ class operator-overloading