【问题标题】:How do I call an object/constructor to return a 2D array如何调用对象/构造函数以返回 2D 数组
【发布时间】:2016-04-05 11:35:53
【问题描述】:

我的测试文件中给出了以下代码来实现:

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

我认为一种解决方案可能是重载我的

std::ostream& operator<<(std::ostream& output, const Matrix& rhs){
output << rhs.data << std::endl;
return output; }

我放 rhs.data 的原因是因为我尝试了 rhs.matrix 和 rhs.p_matrix 但得到一个需要成员变量的错误。在我的 .h 文件中,我唯一允许的成员变量如下:

  1. int noOfRows:存储行数的成员变量
  2. int noOfColumns:存储列数的成员变量
  3. double *data:将地址存储到按列排列的矩阵条目的一维数组的成员变量,即第一列后跟第二列,依此类推 第四
  4. int GetIndex (const int rowIdx, const int columnIdx) const: 成员 确定由 rowIdx 指定的行和由 columnIdx 指定的列中的矩阵条目沿一维数组(数据)的位置(索引)的函数。

我不确定如何仅使用这些变量来使用运算符重载,所以这是最佳解决方案还是有替代方法?考虑到我无法更改测试文件或 4 个成员变量的限制

【问题讨论】:

    标签: c++ arrays constructor operator-overloading


    【解决方案1】:

    如你所说:

    在我的 .h 文件中,我唯一允许的成员变量是 ... double *data:将地址存储到矩阵的一维数组的成员变量

    所以,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 在您的头文件中是公开的,或者使 operator&lt;&lt;(std::ostream&amp; output, const Matrix&amp; rhs) 成为 Matrixfriend(或添加一个 getter)。

    顺便提一下,矩阵通常存储为double*,而不是double**

    【讨论】:

    • 我仍然收到错误“无法在赋值中将'double**'转换为'double*'”,因为我最初需要将数据作为double*,这就是我创建p_matrix的原因。有什么方法可以取消引用我的 double** p_matrix 并使其与 double* 数据相对应?
    • 没办法,您不能将double** 转换为double*。我编辑了我的帖子,以显示您应该如何做,具体取决于您的属性是 double* 还是 double**
    • 谢谢,我已经让数据部分工作了,但是 noOfRows 和 noOfCols 在 operator
    • 哇...第一次写 C++...? ;-) 必须初始化所有属性。我编辑了我的帖子,您需要在构造时设置noOfRowsnoOfColsnoOfColumns(无论它有什么名称)对象属性。
    • 是的,它现在有效,我发布后不久就知道了!您可以编辑您的帖子,以便 1) 引用 double** 和 2) 引用 double* 以避免混淆,因为他们现在都说 double**?另外,你知道我可以在测试.cpp中对静态成员函数使用相同的
    猜你喜欢
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多