arienn
arienn

Reputation: 230

C++ class Matrix, crash

I got here class Matrix, the problem is that it crashes after one line of code: e.g Matrix A(2,2); So it's most likely constructor, but the thing is that when I copied my constructor into other class Matrix it worked just fine... I think I am blind

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

When I use TDM-GGC 32 Bit it works BUT when I change main to:

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;
}

then it's not working again (and it works on similar matrix with my constructors...)

Upvotes: 0

Views: 162

Answers (2)

P0W
P0W

Reputation: 47784

The overloaded operator << should return the ostream object i.e.wyjscie, this enables chaining while using the cout << A << ..., etc.

This is where your code is crashing, so fix as follows :

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

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

Note : There might be other errors too

Upvotes: 1

camomilk
camomilk

Reputation: 763

I'm not sure if this is the source of your problem, but in the destructor you are deleting macierz right before iterating over it and accessing elements. You should iterate over it first, and delete the whole thing afterwards.

Upvotes: 0

Related Questions