【问题标题】:Incorrect output from overloaded [] function重载 [] 函数的错误输出
【发布时间】:2018-02-11 05:14:17
【问题描述】:

规范说函数必须返回由[]中的“行号”指定的矩阵的行

类定义:

class Matrix
{
 public:

 //functions taken out 

 private:
  double ** matrix; // the matrix array
  unsigned rows; // # rows
  unsigned cols; // # columns
};

简短的主要内容:

 cout << "Test []: " << endl;
  try {
   Matrix row = m0[0]; //**m0 is Matrix m0(1,1); where the constructor creates the appropriate array** 
    cout << row << endl;
    row = m0[1];
    cout << row << endl;
    row = m0[100];  // should throw an exception
  } catch (const char * err) {
    cout << err << endl;
  }

函数实现:

 double& Matrix::operator [](const unsigned int &sub)
{   
     if( sub >= rows)
    {
        const char * error = "Error: invalid row index";
        throw error;

    } else
        {       
            return *matrix[sub];

        }


}

重载

//This is working for my other displays so this shouldn't be the problem
ostream &operator << (ostream &ostrm, const Matrix &obj)
{
    //Loop through to display
    for(unsigned int i = 0; i < obj.rows; i++)
    {       
        for(unsigned int j = 0; j< obj.cols; j++)
        {
            ostrm << setw(10)  << setprecision(3) << obj.matrix[i][j]; 
        }

        ostrm << endl;
    }

    return ostrm;
}

重载 = 运算符:

//Again this works for everything else 
Matrix& Matrix::operator=(const Matrix &rhs)
{
    //Copy Rows and Cols
    rows = rhs.rows;
    cols = rhs.cols;

    //If statement to check for self assignment
    if(&rhs == this)
    {
        return *this;
    }
    else 
    {
        delete [] matrix;

        matrix = new double*[rows]; //Allocate Dynamic Array

        //Deep copy elements by looping and copying each element
        for(unsigned int i = 0; i < rows; i++)
        {
            matrix[i] = new double[cols];
            for(unsigned int j = 0; j < cols; j++)
            {
                matrix[i][j] = rhs.matrix[i][j];
            }

        }

        return *this;

    }


}

我的输出:

Test []: 


Error: invalid row index

预期输出:

Test []: 
      17.2        -3      -0.5         6

       8.2         4         3         1

Error: invalid row index

我不确定为什么这些行没有显示或者甚至没有被存储。

提前致谢

【问题讨论】:

  • 请显示Matrix 类定义。例如,matrix 是什么?它是如何声明的?它是如何初始化的?最好尝试创建一个Minimal, Complete, and Verifiable Example
  • 它仍然不是Minimal, Complete, and Verifiable Examplem0 是什么?它是Matrix 对象吗?当operator[] 函数返回对单个double 的引用时,你怎么能做到Matrix row = m0[0]Matrix 有哪些构造函数?您的移动或复制构造函数是否按应有的方式工作?
  • 另外,你关注the rules of three or five吗?或许你应该停止使用指针,改用std::vector,然后改用the rule of zero
  • 矩阵类需要大量时间来构建。你的到处都是错误和设计缺陷。为什么不使用 BLAS,可从 boost 发行版中获得?
  • 错误?设计缺陷?如果我不知道它是错的,我就无法修复它?这是一个校队的任务,它必须实现这个..

标签: c++ overloading operator-keyword subscript


【解决方案1】:

旁白:您的赋值运算符正在泄漏内存:您删除了matrix,但您还需要删除各个行(使用行的原始值) for(unsigned int i = 0; i < rows; i++) delete[] matrix[i];

您的 operator[] 应该返回 double[]double *,而不是 double - 您希望返回整行,而不是单个值。

您的“测试 []”代码甚至不应该编译...Matrix row = m0[0];double 分配给 Matrix 对象。

等等

底线:只需使用Eigen

【讨论】:

  • 因此,如果我将返回类型设为 double*,我究竟应该返回什么,因为我仍然遇到同样的错误?谢谢你的体面的回答!非常感谢
  • 您必须为 operator[] 做出一些选择 - 它可以将代理对象返回到原始矩阵中的数据:这就是 double * 的有效作用,但您可以将其包装在知道行大小的对象,并具有与矩阵交互的运算符; - 它可以返回数据的副本,包装在 Row 对象中; - 它可以返回数据的副本,包装在 Nx1 Matrix 对象中。
  • 抱歉我的无知,但我们还没有介绍代理对象或包装部分。还有其他解决方案吗?
  • 所有这些类型都可以实现与Matrix对象、其他同类型对象或标量的交互,它们可以实现operator&lt;&lt;。所以这主要是一个品味问题,以及你将如何使用你的 Matrix 类。
  • Matrix 类非常简单。这只是第一年的校队作业(这就是我不能使用 Eigen 的原因,我们必须努力克服它)。它实际上只需要返回该行,然后 operator
【解决方案2】:

第一行是错误的。 operator[] 正在返回一个双精度值。你把它分配给一个矩阵。矩阵用一个值初始化。你已经取出了你的构造函数。叫哪一个?我假设,被调用的构造函数将 rows 和 cols 成员初始化为零。当它们为零时,输出流运算符什么也不做。

【讨论】:

  • 即使被调用的构造函数没有初始化成员,也有可能是零。
【解决方案3】:

我设法找到了适合我的问题的解决方案。以下是我实施的,以防其他人遇到类似问题。

Matrix temp(1, cols); //Calls my constructor here

        for(unsigned int i = 0; i < 1; i++)
        {   

            for(unsigned int j = 0; j < cols; j++)
            {
                temp.matrix[i][j] = matrix[sub][j]; //Accessed temp's matrix and assigned it with what is in the matrix that called the function
            }


        }

        return temp;

感谢所有帮助并添加了一些意见的人。非常感谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    相关资源
    最近更新 更多