0
我有问题初始化多维数组元素。初始化多维数组元素
这里是我的代码:
class A{
int *const e;
const int row, column;
public:
A::A(int r, int c) : row(r), column(c), e(new int[r*c])
{
for (int i = 0; i < r*c; i++)
{
e[i] = 0;
}
}
A(const A &matrix) : row(matrix.row), column(matrix.column) ,e(new int[matrix.row*matrix.column])
{
for (int i = 0; i < matrix.row*matrix.column; i++)
{
e[i] = matrix.e[i];
}
}
virtual ~A() //destructing a A
{
delete[] e;
}
};
但是,当我试图初始化多维数组的元素,我得到了一个错误:
int main(int argc, char* argv[])
{
A c(2, 5);
c[0][0] = 1;
A a(c);
return 0;
}
1 IntelliSense: no operator "[]" matches these operands operand types are: MAT [ int ]
编辑: 根据意见我尝试写运算符[]
virtual int *const operator[ ](int r)
{
return e[r][0];
}
它应该获得第r行的第一个元素。但我有一个错误:
1 IntelliSense: expression must have pointer-to-object type
'A'的第二元件如果你想使用它,需要一个'operator []'。 – 2014-12-04 04:04:57