T
Tony Johansson
Hello Experts!!
I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.
I know I must have a copy constructor but I have just cut out that just to
make it easier for you to read.
Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns.
This works fine! So that's not the problem.
I just mentioned it here just to get a better understand of the whole
problem
Because I have dynamically allocated memory I must delete all these
dynamically allocated memory.
The constructor of Matris where I allocate dynamically memory works fine.
Now to my problem here I have written a destructor that I was sure should
work but it doesn't.
Why does not my destructor work because as I believe I have a pointer in
matris[number of rows]to allocated
dynamically memory.
Can you tell me what the problem is?
~Matris() //Destruktor
{
for (int i=0; i< c_rader; i++)
delete matris; //********** Here I get an Debug
assertion failed *************
delete *matris;
}
Many thanks
//Tony
//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}
//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}
Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
matris = new Intvektor*[0];
}
Matris(int rows, int cols) : c_rows(rows), c_columns(cols)
{
matris = new Intvektor*[c_rows];
for (int i=0; i < c_rows; i++)
matris = new Intvektor[c_columns];
}
private:
int c_rows, c_columns;
Intvektor **matris;
};
I have two small classes called Intvektor and Matris shown below and a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically as you can see in the constructor.
I know I must have a copy constructor but I have just cut out that just to
make it easier for you to read.
Class Matris should create a matris by using class Intvektor.
So what I want to have is a pointer to an array of Intvektor and in each
positionindex in this array will I have a pointer to an array of Intvektor
in this way I will create a matris. For example an matris of x rows and y
columns.
This works fine! So that's not the problem.
I just mentioned it here just to get a better understand of the whole
problem
Because I have dynamically allocated memory I must delete all these
dynamically allocated memory.
The constructor of Matris where I allocate dynamically memory works fine.
Now to my problem here I have written a destructor that I was sure should
work but it doesn't.
Why does not my destructor work because as I believe I have a pointer in
matris[number of rows]to allocated
dynamically memory.
Can you tell me what the problem is?
~Matris() //Destruktor
{
for (int i=0; i< c_rader; i++)
delete matris; //********** Here I get an Debug
assertion failed *************
delete *matris;
}
Many thanks
//Tony
//Here in main
//**********
#include "intvektor.h"
#include "matris.h"
int main()
{
Matris a(2,2);
return 0;
}
//Here is class definition of class Intvektor.
//*******************************
class Intvektor
{
public:
Intvektor(int stlk = 0) : size(stlk)
{
array = new int[size];
}
Intvektor& operator=(const Intvektor& v)
{
if (this != &v)
{
delete []array;
size = v.size;
array = new int[size];
for(int i=0; i<size; i++)
array = v.array;
}
return *this;
}
private:
int size;
int* array;
};
//Here is the class definition of class Matris
//********************************
class Matris
{
public:
Matris()
{
matris = new Intvektor*[0];
}
Matris(int rows, int cols) : c_rows(rows), c_columns(cols)
{
matris = new Intvektor*[c_rows];
for (int i=0; i < c_rows; i++)
matris = new Intvektor[c_columns];
}
private:
int c_rows, c_columns;
Intvektor **matris;
};