我有一个二维数组,我想定义一个函数,使用运算符重载来返回用户给我的索引值。
换句话说:
void MyMatrix::ReturnValue()
{
int row = 0, col = 0;
cout << "Return Value From the last Matrix" << endl;
cout << "----------------------------------" << endl;
cout << "Please Enter the index: [" << row << "][" << col << "] =" << ((*this).matrix)[row][col] << endl;
}
< p >操作< code >((*this).matrix)[row][col]应该返回一个< code >int。我不知道如何构建
operator [][]
。或者,我可以将一些调用
operator []
串联起来,但我没有成功,因为对该运算符的第一次调用将返回int*
,而第二次调用将返回int
,这迫使我构建另一个运算符,我不想这样做。
数据矩阵的定义如下:
int** matrix; matrix = new int*[row];
if (matrix == NULL)
{
cout << "Allocation memory - Failed";
}
for (int i = 0; i < row; i++)//Allocation memory
{
matrix[i] = new int[col];
if (matrix[i] == NULL)
{
cout << "Allocation memory - Failed";
return;
}
}
我能做些什么呢?谢谢。
(((*this).matrix)[row])[col]
,那么第一个运算符将返回int*,第二个将返回int。 - LIMAint** matrix; matrix = new int*[row]; if (matrix == NULL) { cout << "Allocation memory - Failed"; } for (int i = 0; i < row; i++)//Allocation memory { matrix[i] = new int[col]; if (matrix[i] == NULL) { cout << "Allocation memory - Failed"; return; } }
- LIMA