C ++类Matrix,崩溃

时间:2015-01-22 01:18:41

标签: c++ matrix crash

我来到这里的类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;
}
然后它再次无法工作(并且它与我的构造函数在类似的矩阵上工作......)

2 个答案:

答案 0 :(得分:1)

重载的operator <<应返回ostream对象,即wyjscie,这样可以在使用cout << A << ...等时进行链接。

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

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

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

注意:可能还有其他错误

答案 1 :(得分:0)

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