【问题标题】:C++ Overloading << operator in Matrix classMatrix类中的C ++重载<<运算符
【发布时间】:2011-05-04 20:29:12
【问题描述】:

我试图在我的 Matrix 类中重载 ostream

token & 之前的预期构造函数、析构函数或类型转换

Matrix::ostream& operator<<(const Matrix& matrix)
{
  for (int r = 0; r < matrix.getNumrows(); r++)
  {
    cout << matrix.getPoint(r, 0);
    for (int c = 0; c < matrix.getNumcolumns(); c++)
    {
      cout << " " << matrix.getPoint(r,c);
    }
    cout << endl;
  }

  return stream;
}

这是我班的其余部分

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "Matrix.h"

using namespace std;

Matrix::Matrix()
{

}

Matrix::Matrix(int rows, int cols) {
    numRows=rows;
    numCols=cols;

    //col=new double[cols];
    mx=new double*[rows];
    for ( int i=0; i < rows; i++ ) {
        mx[i] = new double[cols];
        // initalize each element of the new row.
        for ( int c=0; c < cols; c++ ) {
            mx[i][c] = 0.0;
        }
    }
}


Matrix::Matrix(const Matrix &theMatrix) {
    int rows=theMatrix.numRows;
    int cols=theMatrix.numCols;

    numRows = rows;
    numCols = cols;

    mx=new double*[rows];
    for ( int r=0; r < rows; r++ ) {
        mx[r] = new double[cols];
        // copy each element of the new row.
        for ( int c=0; c < cols; c++ ) {
            mx[r][c] = theMatrix.mx[r][c];

        }
    }
}


void Matrix::setMatrix(string file)
{
    /* read the file */
    fstream inputStream(file.c_str());

    if(inputStream.is_open() )
    {
        string line;
        stringstream ss;

        getline(inputStream, line);
        ss.clear();
        ss.str(line);

        ss >> numRows >> numCols;

        mx=new double*[numRows];
        for ( int i=0; i < numRows; i++ ) {
            mx[i] = new double[numCols];
            // initalize each element of the new row.
            for ( int c=0; c < numCols; c++ ) {
                mx[i][c] = 0.0;
            }
        }


        //now loop to get values
        for(int row=0; row<numRows; row++)
        {
            getline(inputStream, line);
            ss.clear();
            ss.str(line);

            //now get every value in the line
            for(int col=0; col<numCols; col++)
            {
                double current;
                ss >> current;
                mx[row][col] = current;



            }//end reading values of row

        }//end reading rows

    }

    //close the file
    inputStream.close();
}

int Matrix::getNumrows()
{
    return numRows;
}

int Matrix::getNumcolumns()
{
    return numCols;
}

void Matrix::printPoint()
{
    for ( int r=0; r < numRows; r++ )
    {
        for ( int c=0; c < numCols; c++ )
        {
            cout << mx[r][c] << " ";
        }
        cout << endl;
    }
    cout << endl;

}

bool Matrix::getIsSquared()
{
    if( numRows == numCols )
    {
        return true;
    }
    else
    {
        return false;
    }
}

 double Matrix::det()
 {
    double det=0.0;
    if(numRows!=numCols)
    {
        cout << "Number Rows must be same as number Colums\n";
    }

    if(numRows==2)
    {
        det=(mx[0][0]*mx[1][1])-(mx[0][1]*mx[1][0]);
    }
    else
    {
        for(int i=0 ; i<numCols ; i++)
        {
            Matrix temp(numRows-1,numCols-1);
            for(int j=0 ; j<numRows-1 ; j++)
            {
                for(int k=0 ; k<numCols-1 ; k++)
                {
                    if(k<i)
                        temp.mx[j][k]=mx[j+1][k];
                    else
                        temp.mx[j][k]=mx[j+1][k+1];
                }
            }
            det+=pow(-1.0,i)*mx[0][i]*temp.det();
        }
    }
    return det;
 }

 double Matrix::getPoint(int row, int col)
 {
     return mx[row][col];
 }

Matrix Matrix::operator +(const Matrix &right) const
{
    Matrix result(numRows,numCols);
    if ( right.numRows != numRows || right.numCols != numCols )
    {
        cout << "\nError while adding matricies, the two must have the same dimentions.\n";
    }
    else
    {
        for ( int r=0; r < numRows; r++ )
        {
            for ( int c=0; c < numCols; c++ )
            {
                result.mx[r][c] = (this->mx[r][c]) + (right.mx[r][c]);
            }
        }
    }

    return result;
}

【问题讨论】:

  • 当您从输入流中读取数据时,您需要确保处理可能发生的任何错误(例如,如果输入数据与您期望的格式不匹配)。这是通过在每次输入操作后测试流的状态来完成的。你的 C++ 书应该详细介绍它是如何工作的。考虑使用某种形式的智能指针或标准库容器(如std::vector)作为矩阵类使用的存储空间。正如所写,您的类不是异常安全的,您的手动内存管理很可能存在错误。

标签: c++ matrix operator-overloading ostream


【解决方案1】:

只是改变

Matrix::ostream& operator<<(const Matrix& matrix)

std::ostream& operator<< (std::ostream &stream, const Matrix &matrix)

它将是一个独立的功能......并且应该可以正常工作。

【讨论】:

    【解决方案2】:

    写成:

    ostream& operator<<(ostream& os, const Matrix& matrix) 
    { 
        for (int r = 0; r < matrix.getNumrows(); r++) 
        { 
            os << matrix.getPoint(r, 0); 
            for (int c = 0; c < matrix.getNumcolumns(); c++) 
            { 
                os << " " << matrix.getPoint(r,c); 
            } 
            os << endl; 
        } 
        return os; 
    } 
    

    它会起作用的。它不必是 Matrix 的成员函数。

    【讨论】:

    • 解决了这个问题,但现在我得到了:
    • 错误:将“const Matrix”作为“int Matrix::getNumrows()”的“this”参数传递会丢弃限定符|
    • @Tony:如果你想在一个 const 限定的对象上调用一个成员函数,你需要对这个成员函数进行 const 限定。
    【解决方案3】:

    如果你想为你的类重载 ostream operator&lt;&lt;,你需要使用友元函数或非成员函数,因为 ostream 对象出现在表达式的左侧(它被写成如os &lt;&lt; my_matrix)。

    std::ostream& operator<<(std::ostream& os, const Matrix& matrix) { /* ... */ }
    

    看起来就像你试图将它实现为一个成员函数,但实际上应该看起来像:

    std::ostream& Matrix::operator<<(const Matrix& matrix) { /* ... */ }
    

    这不起作用,因为当您将运算符重载实现为成员函数时,表达式左侧的对象类型与重载所属的类的类型相同 (所以,在这种情况下,你必须写my_matrix1 &lt;&lt; my_matrix2,这不是你想要的)。

    在重载中,你不应该直接写信给cout;您应该写入作为参数传递给函数的ostream 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多