C++超载<< Matrix 类中的运算符

发布于 2024-09-30 16:47:21 字数 4438 浏览 6 评论 0 原文

我正在尝试使 ostream 超载 <<我的 Matrix 类中的运算符,但我不断收到以下错误:

Expected constructor, destructor, or type conversion before 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;
}

I am trying to overload the ostream << operator in my Matrix class, but I keep getting the following error:

Expected constructor, destructor, or type conversion before 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;
}

This is the rest of my class

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2024-10-07 16:47:21

如果要为类重载 ostream 运算符<<,则需要使用友元函数或非成员函数,因为 ostream 对象出现在表达式的左侧(写为 os << my_matrix)。

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

看起来就像您试图将其实现为成员函数,但实际上应该如下所示:

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

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

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

If you want to overload the ostream operator<< for your class, you need to use either a friend function or a non-member function because the ostream object appears on the left-hand side of the expression (it's written as os << my_matrix).

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

It looks like you are trying to implement it as a member function, but that should actually look like:

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

This won't work because when you implement an operator overload as a member function, the type of the object on the left hand side of the expression is the same as the type of the class of which the overload is a member (so, in this case, you'd have to write my_matrix1 << my_matrix2, which isn't what you want).

Inside of the overload, you shouldn't write to cout directly; you should write to the ostream object that is passed as an argument to the function.

窝囊感情。 2024-10-07 16:47:21

写成:

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 的成员函数。

Write it as:

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

and it will work. It does not have to be a member function of Matrix.

随梦而飞# 2024-10-07 16:47:21

只需更改

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

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

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

Just change

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

to

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

It'll be a stand-alone function .. and should work just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文