=矩阵类的运算符表现得很奇怪

时间:2013-05-10 22:01:22

标签: c++ class operators operator-keyword

CPP

#include "del2.h"

Matrix::Matrix()
{
    dArray = NULL;
}
bool Matrix::isValid() const
{
    if (dArray == NULL)
        return false;
    return true;
}
Matrix::~Matrix()
{
    delete [] dArray;
}
Matrix::Matrix(unsigned int nRows)
{
    rows = nRows;
    columns = nRows;
    dArray = new double[nRows * nRows];
    for (unsigned int i = 0; i < nRows; i++)
    {
        for (unsigned int n = 0; n < nRows; n++)
        {
            at(i,n) = 0;
        }
    }
    at(0,0) = 1;
    at(rows-1,columns-1) = 1;
}
Matrix::Matrix(unsigned int nRows, unsigned int nColumns)
{
    dArray = new double[nRows * nColumns];
    rows = nRows;
    columns = nColumns;
    for (unsigned int i = 0; i < nRows; i++)
        for (unsigned int n = 0; n < nColumns; n++)
            dArray[i * columns + n] = 0;


}
const Matrix Matrix::operator =(const Matrix & rhs)
{
    columns = rhs.getColumns();
    rows = rhs.getRows();
    delete [] dArray;
    dArray = new double[rows * columns];
    for (int row = 0; row < rows; row++)
        for (int column = 0; column < columns; column++)
            at(row,column) = rhs.at(row,column);


    return *this;
}
std::ostream & operator <<( std::ostream & out, const Matrix & classPrint )
{
    if (!classPrint.isValid())
        return out;
    int rows = classPrint.getRows();
    int columns = classPrint.getColumns();
    out << std::endl;
    for (int i = 0; i < rows; i++)
     {
        out << "| ";
        for (int n = 0; n < columns; n++)
            out << classPrint.at(i,n) << " ";
        out << "|" << std::endl;
    }
    out << endl;
    return out;
}

HEADER:

#ifndef DEL2
#define DEL2
#include <iostream>
using namespace std;

class Matrix
{
private:
    int rows;
    int columns;
    double * dArray;
public:
    ~Matrix();
    Matrix();
    explicit Matrix(unsigned int nRows);
    Matrix(unsigned int nRows, unsigned int nColumns);
    const Matrix operator =(const Matrix & rhs);

    const double at(int row, int column) const
    { return dArray[ row*this->columns + column ]; }
    double & at( int row, int column )
    { return dArray[ row*this->columns + column ]; }

    const int getRows() const {return rows;}
    const int getColumns() const {return columns;}

    bool isValid() const;
};
std::ostream & operator <<( std::ostream & out, const Matrix & classPrint );

#endif // MATRIX

主:

#include <iostream>
#include "del2.h"

using namespace std;

int main()
{
    Matrix A;
    Matrix B(10);
    A = B;
    cout << A;
    return 0;
}

当我运行时,会发生以下情况:

第一个索引,A的矩阵[0],总是变成一些奇怪的数字,如2.22323e-306。我不明白为什么。即使我尝试设置“at(0,0)= 1;”在operator = - 循环后的函数中,它仍然没有0.

2 个答案:

答案 0 :(得分:0)

小心操作员=不正确

Matrix & operator = (const Matrix & rhs){
    if (&rhs != this){ 
     // Your stuff here
    }
    return *this;
}

未定义在空数组上调用delete []。它会崩溃。 您还必须定义复制构造函数

答案 1 :(得分:0)

问题出在这里

const Matrix operator =(const Matrix & rhs);

应该是

const Matrix& operator =(const Matrix & rhs);

,定义应为

const Matrix& Matrix::operator =(const Matrix & rhs)
{
   if (&rhs == this)
      return *this;

其他operator= 返回Matrix 的副本,其副本dArray与原始矩阵相同。当临时返回的数组超出范围时,您的数据将为deleted

相关问题