C++ class 矩阵,崩溃
C++ class Matrix, crash
我到了class Matrix,问题是它在一行代码后崩溃:例如Matrix A(2,2);
所以它很可能是构造函数,但问题是当我将我的构造函数复制到其他 class 矩阵时它工作得很好......我想我是盲人
#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;
}
然后它就不能再工作了(而且它与我的构造函数在类似的矩阵上工作...)
我不确定这是否是您问题的根源,但在析构函数中,您在迭代它并访问元素之前删除了 macierz。你应该先遍历它,然后再删除整个东西。
重载的 operator <<
应该 return ostream
对象,即 wyjscie
,这会在使用 cout << A << ...
等时启用链接
这是您的代码崩溃的地方,因此请按如下方式修复:
ostream & operator<< (ostream& wyjscie, Matrix& co)
{
//...
wyjscie << endl;
return wyjscie ; // <---- Notice this
}
注意 : 可能还有其他错误
我到了class Matrix,问题是它在一行代码后崩溃:例如Matrix A(2,2); 所以它很可能是构造函数,但问题是当我将我的构造函数复制到其他 class 矩阵时它工作得很好......我想我是盲人
#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;
}
然后它就不能再工作了(而且它与我的构造函数在类似的矩阵上工作...)
我不确定这是否是您问题的根源,但在析构函数中,您在迭代它并访问元素之前删除了 macierz。你应该先遍历它,然后再删除整个东西。
重载的 operator <<
应该 return ostream
对象,即 wyjscie
,这会在使用 cout << A << ...
等时启用链接
这是您的代码崩溃的地方,因此请按如下方式修复:
ostream & operator<< (ostream& wyjscie, Matrix& co)
{
//...
wyjscie << endl;
return wyjscie ; // <---- Notice this
}
注意 : 可能还有其他错误