【问题标题】:= operator for a matrix class behaving weird= 行为怪异的矩阵类的运算符
【发布时间】:2013-05-10 22:01:22
【问题描述】:

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

标题:

#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 的第一个索引 matrix[0] 总是变成一些奇怪的数字,例如 2.22323e-306。我不明白为什么。即使我尝试设置“at(0,0) = 1;”在运算符 = - 循环后的函数中,它仍然没有 0。

【问题讨论】:

  • 使用浮点,我不会感到惊讶。对于所有意图和目的,这基本上是 0。它会导致任何其他错误吗?
  • 不,我不知道。它运行良好,一切正常,只是一个错误的值。

标签: c++ class operators operator-keyword


【解决方案1】:

问题来了

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= 将返回矩阵的副本,其dArray 与原始矩阵相同。当临时返回的数组超出范围时,您的数据将为deleted。

【讨论】:

    【解决方案2】:

    小心操作符 = 不正确

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

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

    【讨论】:

      猜你喜欢
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 2016-04-14
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多