gpt4 book ai didi

C++ 在 Matrix 类中重载 << 运算符

转载 作者:行者123 更新时间:2023-11-27 22:29:52 25 4
gpt4 key购买 nike

我试图在我的 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;
}

最佳答案

如果你想重载ostream operator<<对于您的类(class),您需要使用友元函数或非成员函数,因为 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作为参数传递给函数的对象。

关于C++ 在 Matrix 类中重载 << 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4093440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com