gpt4 book ai didi

C++ 类 Matrix,崩溃

转载 作者:行者123 更新时间:2023-11-28 06:28:37 27 4
gpt4 key购买 nike

我在这里找到了 Matrix 类,问题是它在一行代码后崩溃:例如 Matrix A(2,2);所以它很可能是构造函数,但问题是当我将我的构造函数复制到其他类 Matrix 时它工作得很好......我想我是瞎子

#include<iostream>
#include<cmath>
#include<string>
#include<fstream>

using namespace std;

class Matrix
{
public:
Matrix(int, int);
Matrix(const Matrix& copyMatrix);
~Matrix();
//Matrix(const char *sciezka);
Matrix& mac_cin(string);
friend ostream& operator<< (ostream&, Matrix&);
Matrix& operator+= (const Matrix&);
Matrix& operator-= (const Matrix&);
Matrix& operator*= (const Matrix&);
Matrix& operator= (const Matrix&);
friend Matrix operator* (const Matrix & left, const Matrix & right);
friend Matrix operator+ (const Matrix & left, const Matrix & right);
friend Matrix operator- (const Matrix & left, const Matrix & right);
class RangeError{};
class AllocError{};
class OpenError{};
class IncorrectSize{};
private:
double **macierz;
unsigned int wiersze, kolumny;
};

Matrix::Matrix(int x = 1, int y = 1): wiersze(x), kolumny(y)
{

if (wiersze < 1 || kolumny < 1)
{
throw AllocError();
}
macierz = new double*[wiersze];

for (unsigned i = 0; i < wiersze; i++)
{
macierz[i] = new double[kolumny];
for (unsigned j = 0; j < kolumny; j++)
{
macierz[i][j] = 0;
}
}
}

Matrix::Matrix(const Matrix& copyMatrix)
{
wiersze=copyMatrix.wiersze;
kolumny=copyMatrix.kolumny;

macierz = new double*[wiersze];

for (unsigned i = 0; i < wiersze; i++)
{
macierz[i] = new double[kolumny];
for (unsigned j = 0; j < kolumny; j++)
{
macierz[i][j] = copyMatrix.macierz[i][j];
}
}
}

Matrix::~Matrix()
{
delete [] macierz;
for (unsigned i = 0; i < wiersze; i++)
{
delete [] macierz[i];
}
}
/*
Matrix::Matrix(const char *sciezka)
{
ifstream plik(sciezka);
if (plik.good() != true)
{
throw OpenError();
}
plik >> wiersze >> kolumny;
macierz = new double*[wiersze];

for (unsigned i = 0; i < wiersze; i++)
{
for (unsigned j = 0; j < kolumny; j++)
{
plik >> macierz[i][j];
}
}
//delete [] *macierz;
//delete [] macierz;
}
*/
ostream & operator<< (ostream& wyjscie, Matrix& co)
{
for (unsigned i = 0; i < co.wiersze; i++)
{
for (unsigned j = 0; j < co.kolumny; j++)
{
wyjscie << co.macierz[i][j] << " ";
}
cout << endl;
}
}

Matrix operator* (const Matrix & left, const Matrix & right)
{
Matrix nlr(left);
return nlr *= right;
}

Matrix operator+ (const Matrix & left, const Matrix & right)
{
Matrix nlr(left);
return nlr += right;
}

Matrix operator- (const Matrix & left, const Matrix & right)
{
Matrix nlr(left);
return nlr -= right;
}

Matrix& Matrix::operator+=(const Matrix& co)
{
if(this->wiersze!=co.wiersze || this->kolumny!=co.kolumny)
{
throw IncorrectSize{};
}

for(unsigned i=0; i<this->wiersze; i++)
{
for(unsigned j=0; j<this->kolumny; j++)
{
this->macierz[i][j] = this->macierz[i][j]+co.macierz[i][j];
}
}
return *this;
}

Matrix& Matrix::operator-=(const Matrix& co)
{
if(this->wiersze!=co.wiersze || this->kolumny!=co.kolumny)
{
throw IncorrectSize{};
}

for(unsigned i=0; i<this->wiersze; i++)
{
for(unsigned j=0; j<this->kolumny; j++)
{
this->macierz[i][j] = this->macierz[i][j]-co.macierz[i][j];
}
}
return *this;
}

Matrix& Matrix::operator*=(const Matrix& co)
{
if(this->wiersze!=co.wiersze || this->kolumny!=co.kolumny)
{
throw IncorrectSize{};
}

for(unsigned i=0; i<this->wiersze; i++)
{ // moze double temp=0;
for(unsigned j=0; j<this->kolumny; j++)
{
this->macierz[i][j] = this->macierz[i][j]*co.macierz[i][j]; // temp+=
}
} // moze newMatrix.macierz[i][j] = temp;
return *this;
}

Matrix& Matrix::operator=(const Matrix& co)
{
if(this->wiersze!=co.wiersze || this->kolumny!=co.kolumny)
{
throw IncorrectSize{};
}
for(unsigned i=0; i<this->wiersze; i++)
{
for(unsigned j=0; j<this->kolumny; j++)
{
this->macierz[i][j] = co.macierz[i][j];
}
}
return *this;
}

Matrix& Matrix::mac_cin(string mac) {
int i,j;
cout << "Podaj zawartosc macierzy\n";
for(i=0; i<this->wiersze; i++)
{
for(j=0; j<this->kolumny; j++)
{
cout << mac << "[" << i << "][" << j << "] = ";
cin >> this->macierz[i][j];
}
}
return *this;
}

int main()
{

Matrix A(2,2);
A.mac_cin("A");
Matrix okA(A);

return 0;
}

当我使用 TDM-GGC 32 位时,它可以工作,但是当我将 main 更改为:

int main()
{

Matrix A(2,2);
A.mac_cin("A");
Matrix okA(A);

Matrix B(3,3);
B.mac_cin("B");
Matrix okB(B);
Matrix C(3,3);
Matrix okC(C);
cout << endl;
cout << "A: " << endl << A << endl << endl;
cout << "B: " << endl << B << endl << endl;

C=A+B;
cout << "A+B: " << endl << C << endl << endl;
C=A-B;
cout << "A-B: " << endl << C << endl << endl;
C=A*B;
cout << "A*B: " << endl << C << endl << endl;
C=A;
C+=B;
cout << "A+=B: " << endl << C << endl << endl;
C=A;
C-=B;
cout << "A-=B: " << endl << C << endl << endl;
C=A;
C*=B;
cout << "A*=B: " << endl << C << endl << endl;



system("PAUSE");
return 0;
}

然后它就不能再工作了(而且它在与我的构造函数类似的矩阵上工作...)

最佳答案

重载operator <<应该返回 ostream对象即 wyjscie ,这会在使用 cout << A << ... 时启用链接等

这是您的代码崩溃的地方,因此请按如下方式修复:

ostream & operator<< (ostream& wyjscie, Matrix& co)
{
//...
wyjscie << endl;

return wyjscie ; // <---- Notice this
}

注意:可能还有其他错误

关于C++ 类 Matrix,崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28079842/

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