469,160 Members | 1,819 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

Deletion of a matrix

In one of my header-files I have a class named "matrix" with the function to
construct a matrix. It works properly but at the time of destruction of the
class the program stops. What have I done wrong? Thanks in advance Dieter

class matrix

{

public:

matrix* next;

int xDim;

int yDim;

int **field;

~ matrix()

{

for (int line=0; line<xDim; line++)

delete [] field[line];

delete [] field;

// delete next;

};

matrix::matrix(int x, int y)

{

int i, j;

xDim=x;

yDim=y;

field = new int* [xDim];

for(i=0; i<xDim; i++)

field[i] = new int [yDim];

for(i=0; i<xDim; i++)

for(j=0; j<yDim; j++)

field[i][j]=INT_MAX;

}

void Setnext (matrix* ne) {next=ne;}

matrix* Getnext () {return next;}

void Setmatrix (int Lines, int Splits, int w)

{

field[Lines][Splits]=w;

}

int Getmatrix (int Lines, int Splits)

{

return field[Lines][Splits];

}

};
Jul 22 '05 #1
3 1300
Huibuh wrote:
In one of my header-files I have a class named "matrix" with the function to
construct a matrix. It works properly but at the time of destruction of the
class the program stops. What have I done wrong? Thanks in advance Dieter
[...]


You didn't post any code that shows how you use your matrix. So, I am
guessing here, but most likely you didn't follow the "Rule of Three".
Read about the Rule of Three in the FAQ or in the newsgroup archives
(http://groups.google.com)

V
Jul 22 '05 #2

"Huibuh" <no*****@t-online.de> wrote in message
news:cl*************@news.t-online.com...
In one of my header-files I have a class named "matrix" with the function
to
construct a matrix. It works properly but at the time of destruction of
the
class the program stops. What have I done wrong? Thanks in advance Dieter


You've failed to define either a copy constructor or an assignment operator
when for your class they are desperately needed. Look these up in your
favourite C++ book.

john
Jul 22 '05 #3
"Huibuh" <no*****@t-online.de> wrote:
In one of my header-files I have a class named "matrix" with the function to
construct a matrix. It works properly but at the time of destruction of the
class the program stops. What have I done wrong? Thanks in advance Dieter
In your destructor you have the line:
// delete next;
This line (if it were not commented out) is a problem
because you never assign 'next' in your constructor.
(I think you wanted next = NULL there).

In fact this class is badly designed because you are mixing
the matrix with the implementation of a matrix container.
If you ever change your container requirements (eg. if you
want reverse linking) then you have to change the matrix
class. Instead you should have the matrix and the container
separate.

You could avoid all your problems by using standard containers
and memory management:

class matrix
{
std::vector< std::vector<int> > field;
etc.

and then use std::list<matrix> when you want a list.

};

matrix::matrix(int x, int y)

{

int i, j;

xDim=x;

yDim=y;

field = new int* [xDim];

for(i=0; i<xDim; i++)

field[i] = new int [yDim];

for(i=0; i<xDim; i++)

for(j=0; j<yDim; j++)

field[i][j]=INT_MAX;

}

void Setnext (matrix* ne) {next=ne;}

matrix* Getnext () {return next;}

void Setmatrix (int Lines, int Splits, int w)

{

field[Lines][Splits]=w;

}

int Getmatrix (int Lines, int Splits)

{

return field[Lines][Splits];

}

};

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by A_Republican | last post: by
20 posts views Thread by Frank-O | last post: by
2 posts views Thread by DarrenWeber | last post: by
reply views Thread by DarrenWeber | last post: by
18 posts views Thread by Hypnotik | last post: by
2 posts views Thread by rijaalu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.