来自重载[]函数的输出不正确

时间:2017-09-02 08:48:32

标签: c++ overloading operator-keyword subscript

规范说该函数必须返回由"行号"指定的矩阵的行。在[]

班级定义:

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];

        }


}

重载&lt;&lt;显示操作员:

//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

我不确定为什么行没有显示或甚至可能没有存储。

提前致谢

3 个答案:

答案 0 :(得分:0)

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

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

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

底线:只需使用Eigen

答案 1 :(得分:0)

第一行错了。 operator []返回一个double。您将其分配给矩阵。矩阵初始化为一个值。你已经取出了你的结构。叫哪一个?我假设,被调用的构造函数用零初始化rows和cols成员。当它们为零时,输出流操作符不执行任何操作。

答案 2 :(得分:0)

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

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;

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