如何调用 object/constructor 到 return 二维数组
How do I call an object/constructor to return a 2D array
我在我的测试文件中得到了以下代码来实现:
cout << "Testing the Matrix constructors:" << endl;
cout << "Case 1: Creating a 2x4 matrix of zeros with the standard constructor:" << endl;
{
Matrix matrix(2, 4);
cout << matrix << endl;
目前我的构造函数.cpp文件中的代码如下:
Matrix::Matrix (const int noOfRows, const int noOfCols){
double **p_matrix = new double*[noOfRows];
for(int i=0; i< noOfRows; i++){
p_matrix[i] = new double[noOfCols];
}
for(int i=0; i< noOfRows; i++){
for(int j=0; j<noOfCols; j++){
p_matrix[i][j] = 0;
}
}
我的主要困惑是代码的 cout<< 矩阵部分,因为我可以在我的构造函数中打印出我的 2x4 矩阵而不需要这一行。但是,我被要求包括 cout<< 矩阵,我不确定我是否理解它是如何工作的。它在调用我的对象矩阵吗?如果是这样,我如何 return 我的二维数组 p_matrix 因为我无法 return 来自构造函数的值?
我认为一种解决方案可能是重载我的 << 运算符,如下所示:
std::ostream& operator<<(std::ostream& output, const Matrix& rhs){
output << rhs.data << std::endl;
return output; }
我放rhs.data的原因是因为我尝试了rhs.matrix和rhs.p_matrix但是得到了一个错误,需要一个成员变量。在我的 .h 文件中,我唯一允许的成员变量如下:
- int noOfRows:存储行数的成员变量
- int noOfColumns:存储列数的成员变量
- double *data:成员变量,用于存储按列排列的矩阵条目的一维数组的地址,即第 1 列后跟第 2 列,依此类推
第四
- int GetIndex (const int rowIdx, const int columnIdx) const: 成员
确定 rowIdx 指定的行和 columnIdx 指定的列中矩阵条目在一维数组(数据)中的位置(索引)的函数。
我不确定如何仅使用这些变量来使用运算符重载,所以这是最好的解决方案还是有替代方法?考虑到我无法更改测试文件或 4 个成员变量的限制
如你所说:
Within my .h file, the only member variables I'm allowed are ... double *data: the member variable that stores the address to the 1-D array of the matrix
因此,Matrix
构造函数应该初始化 data
属性,而不是局部 double **p_matrix
变量(然后 data
未初始化)...
只需替换:
Matrix::Matrix (const int noOfRows, const int noOfCols)
{
double **p_matrix = new double*[noOfRows];
for(int i=0; i< noOfRows; i++){
p_matrix[i] = new double[noOfCols];
}
for(int i=0; i< noOfRows; i++){
for(int j=0; j<noOfCols; j++){
p_matrix[i][j] = 0;
}
}
}
作者:
1.如果您的 data
属性是 double**
Matrix::Matrix (const int noOfRows, const int noOfCols)
{
this->noOfRows = noOfRows;
this->noOfCols = noOfCols;
data = new double*[noOfRows];
for(int i=0; i< noOfRows; i++){
data[i] = new double[noOfCols];
}
for(int i=0; i< noOfRows; i++){
for(int j=0; j<noOfCols; j++){
data[i][j] = 0;
}
}
}
稍后,您可以:
std::ostream& operator<<(std::ostream& output, const Matrix& rhs)
{
for( int i=0; i< noOfRows; i++){
for( int j=0; j < noOfCols; j++){
output << rhs.data[i][j] << " "; // next column
}
output << std::endl; // next line
}
return output;
}
2。如果您的 data
属性是 double*
Matrix::Matrix (const int noOfRows, const int noOfCols){
this->noOfRows = noOfRows;
this->noOfCols = noOfCols;
data = new double[noOfRows*noOfCols];
for(int i=0; i< noOfRows*noOfCols; i++){
data[i] = 0;
}
}
稍后,您可以:
std::ostream& operator<<(std::ostream& output, const Matrix& rhs)
{
for( int i=0; i< noOfRows; i++){
for( int j=0; j < noOfCols; j++){
output << rhs.data[noOfCols*i+j] << " "; // next column
}
output << std::endl; // next line
}
return output;
}
在这两种情况下,请确保头文件中的 data
为 public,或使 operator<<(std::ostream& output, const Matrix& rhs)
为 Matrix
的 friend
(或添加getter).
顺便说一句,请注意矩阵通常存储为 double*
而不是 double**
。
我在我的测试文件中得到了以下代码来实现:
cout << "Testing the Matrix constructors:" << endl;
cout << "Case 1: Creating a 2x4 matrix of zeros with the standard constructor:" << endl;
{
Matrix matrix(2, 4);
cout << matrix << endl;
目前我的构造函数.cpp文件中的代码如下:
Matrix::Matrix (const int noOfRows, const int noOfCols){
double **p_matrix = new double*[noOfRows];
for(int i=0; i< noOfRows; i++){
p_matrix[i] = new double[noOfCols];
}
for(int i=0; i< noOfRows; i++){
for(int j=0; j<noOfCols; j++){
p_matrix[i][j] = 0;
}
}
我的主要困惑是代码的 cout<< 矩阵部分,因为我可以在我的构造函数中打印出我的 2x4 矩阵而不需要这一行。但是,我被要求包括 cout<< 矩阵,我不确定我是否理解它是如何工作的。它在调用我的对象矩阵吗?如果是这样,我如何 return 我的二维数组 p_matrix 因为我无法 return 来自构造函数的值?
我认为一种解决方案可能是重载我的 << 运算符,如下所示:
std::ostream& operator<<(std::ostream& output, const Matrix& rhs){
output << rhs.data << std::endl;
return output; }
我放rhs.data的原因是因为我尝试了rhs.matrix和rhs.p_matrix但是得到了一个错误,需要一个成员变量。在我的 .h 文件中,我唯一允许的成员变量如下:
- int noOfRows:存储行数的成员变量
- int noOfColumns:存储列数的成员变量
- double *data:成员变量,用于存储按列排列的矩阵条目的一维数组的地址,即第 1 列后跟第 2 列,依此类推 第四
- int GetIndex (const int rowIdx, const int columnIdx) const: 成员 确定 rowIdx 指定的行和 columnIdx 指定的列中矩阵条目在一维数组(数据)中的位置(索引)的函数。
我不确定如何仅使用这些变量来使用运算符重载,所以这是最好的解决方案还是有替代方法?考虑到我无法更改测试文件或 4 个成员变量的限制
如你所说:
Within my .h file, the only member variables I'm allowed are ... double *data: the member variable that stores the address to the 1-D array of the matrix
因此,Matrix
构造函数应该初始化 data
属性,而不是局部 double **p_matrix
变量(然后 data
未初始化)...
只需替换:
Matrix::Matrix (const int noOfRows, const int noOfCols)
{
double **p_matrix = new double*[noOfRows];
for(int i=0; i< noOfRows; i++){
p_matrix[i] = new double[noOfCols];
}
for(int i=0; i< noOfRows; i++){
for(int j=0; j<noOfCols; j++){
p_matrix[i][j] = 0;
}
}
}
作者:
1.如果您的 data
属性是 double**
Matrix::Matrix (const int noOfRows, const int noOfCols)
{
this->noOfRows = noOfRows;
this->noOfCols = noOfCols;
data = new double*[noOfRows];
for(int i=0; i< noOfRows; i++){
data[i] = new double[noOfCols];
}
for(int i=0; i< noOfRows; i++){
for(int j=0; j<noOfCols; j++){
data[i][j] = 0;
}
}
}
稍后,您可以:
std::ostream& operator<<(std::ostream& output, const Matrix& rhs)
{
for( int i=0; i< noOfRows; i++){
for( int j=0; j < noOfCols; j++){
output << rhs.data[i][j] << " "; // next column
}
output << std::endl; // next line
}
return output;
}
2。如果您的 data
属性是 double*
Matrix::Matrix (const int noOfRows, const int noOfCols){
this->noOfRows = noOfRows;
this->noOfCols = noOfCols;
data = new double[noOfRows*noOfCols];
for(int i=0; i< noOfRows*noOfCols; i++){
data[i] = 0;
}
}
稍后,您可以:
std::ostream& operator<<(std::ostream& output, const Matrix& rhs)
{
for( int i=0; i< noOfRows; i++){
for( int j=0; j < noOfCols; j++){
output << rhs.data[noOfCols*i+j] << " "; // next column
}
output << std::endl; // next line
}
return output;
}
在这两种情况下,请确保头文件中的 data
为 public,或使 operator<<(std::ostream& output, const Matrix& rhs)
为 Matrix
的 friend
(或添加getter).
顺便说一句,请注意矩阵通常存储为 double*
而不是 double**
。