C++类矩阵,崩溃

C++ class Matrix, crash

本文关键字:崩溃 C++      更新时间:2023-10-16

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

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

注意:可能还有其他错误

我不确定这是否是您问题的根源,但是在析构函数中,您在迭代它并访问元素之前删除了macierz。您应该先迭代它,然后再删除整个内容。