Class Matrix not working
Dec 8, 2012 at 7:57pm UTC
Hello,
I'm new to C++ and just started my trip with it.
I m trying to write a class of Matrix which is NxN dimension. I have to implement constructor, destructor, adding, multiplying operators etc.
I wrote it and I have a problem with adding operator. It's not working, however I have no idea where exactly this function is broken after analyizing the code and debugging. It builds solution...
Thank you for any help and hints. :)
This is code:
class Matrix
{
double **tab;
int dimension;
public:
Matrix ();
~Matrix ();
Matrix (int n);
void set(double a);
void print();
int getwymiar() {return dimension;}
Matrix operator+ (Matrix A);
};
#include <iostream>
#include "declaration.h"
using namespace std;
Matrix::Matrix ()
{
**tab=NULL;
dimension=0;
}
Matrix::~Matrix()
{
if (tab!=NULL)
{
for (int i = 0; i<dimension; i++)
delete [] tab[i];
delete []tab;
}
tab=NULL;
}
Matrix::Matrix (int n)
{
dimension=n;
tab=new double *[n];
for (int i=0; i<n; i++)
tab[i]=new double[n];
}
void Matrix::set(double a)
{
for(int j=0;j<dimension;j++)
for(int i=0;i<dimension;i++)
tab[i][j]=a;
}
void Matrix::print()
{
for(int i=0;i<dimension;i++)
{
for(int j=0;j<dimension;j++)
cout << tab[i][j] << "\t";
cout << "\n";
}
cout << "\n";
}
Matrix Matrix::operator +(Matrix A)
{
Matrix Sum(dimension);
for(int i=0;i<dimension;i++)
{
for(int j=0;j<dimension;j++)
{
Sum.tab[i][j]=tab[i][j]+ A.tab[i][j];
}
}
return Sum;
}
Dec 8, 2012 at 8:31pm UTC
**tab=NULL;
This is not correct. You are dereferencing an uninitialized pointer. I think what you want is
tab=NULL;
.
Your code will not work if you try to add two Matrices with different dimensions.
Your class needs a copy constructor so that the object can be copied correctly. It should probably have a copy assignment operator as well. See the rule of three:
http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29
Dec 8, 2012 at 10:24pm UTC
I rewrote whole program.
Now i have :
#pragma once
class Matrix
{
private:
double **tab;
int dimensionX;
int dimensionY;
void Allocation();
public:
Matrix (int DimenX, int DimenY);
Matrix ();
~Matrix();
Matrix (const Matrix& M);
Matrix& operator= (const Matrix& M);
Matrix& operator+ (const Matrix& M);
Matrix& operator-= (const Matrix& M);
Matrix& operator+= (const Matrix& M);
void set(double a);
void print();
};
#include <iostream>
#include "declarations.h"
using namespace std;
void Matrix::Allocation()
{
tab=new double*[dimensionX];
for (int i=0; i<dimensionX; i++)
{
tab[i]=new double[dimensionY];
}
}
Matrix::Matrix(int DimenX, int DimenY):dimensionX(DimenX),dimensionY(DimenY)
{
Allocation();
for (int i=0;i<dimensionX;i++)
{
for (int j=0;j<dimensionY;j++)
tab[i][j]=0;
}
}
Matrix::Matrix()
{
tab=NULL;
dimensionX=0;
dimensionY=0;
}
Matrix::Matrix(const Matrix& M): dimensionX(M.dimensionX), dimensionY(M.dimensionY)
{
Allocation();
for(int i=0;i<dimensionX;i++)
{
for(int j=0;j<dimensionY;j++)
tab[i][j]=M.tab[i][j];
}
}
Matrix::~Matrix()
{
for(int i=0;i<dimensionX;i++)
{
delete [] tab[i];
}
delete[] tab;
}
Matrix &Matrix::operator= (const Matrix& M)
{
if(this == &M)
{
return *this;
}
else
{
if (dimensionX !=M.dimensionX || dimensionY !=M.dimensionY)
{
this->~Matrix();
dimensionX=M.dimensionX;
dimensionY=M.dimensionY;
Allocation();
}
for(int i=0;i<dimensionX;i++)
{
for(int j=0;j<dimensionY;j++)
tab[i][j]=M.tab[i][j];
}
}
return *this;
}
Matrix &Matrix::operator+= (const Matrix& M)
{
for(int i=0;i<dimensionX;i++)
{
for(int j=0;j<dimensionY;j++)
tab[i][j]+= M.tab[i][j];
}
return *this;
}
Matrix &Matrix::operator+ (const Matrix& M)
{
Matrix temp(*this);
return (temp+=M);
}
Matrix &Matrix::operator-= (const Matrix& M)
{
for(int i=0;i<dimensionX;i++)
{
for(int j=0;j<dimensionY;j++)
tab[i][j]-=M.tab[i][j];
}
return *this;
}
void Matrix::set(double a)
{
for(int j=0;j<dimensionX;j++)
{
for(int i=0;i<dimensionY;i++)
tab[i][j]=a;
}
}
void Matrix::print()
{
for(int i=0;i<dimensionX;i++)
{
for(int j=0;j<dimensionY;j++)
cout << tab[i][j] << "\t";
cout << "\n";
}
cout << "\n";
}
+= is working,
but still + is not working.
I assume its because line:
Matrix temp(*this);
Dec 8, 2012 at 10:34pm UTC
operator+ should not return a reference.
Matrix operator + (const Matrix& M);
Dec 8, 2012 at 10:36pm UTC
I love you :*
Thank you !
Topic archived. No new replies allowed.