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.
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
Now to my problem I run into problem because my program krasch see below
where I have
written "Here does the program krasch " in the constructor of class Matris.
The problem is something with pointer. I assume that I must have a pointer
to pointer that's way I have declared it
in this way Intvektor **matris; as you can see in the private section of
class Matris.
I hope you can tell me how I should declare matris variable to be able to
crete a matris.
//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[i] = v.array[i];
}
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 rad, int kol) : c_rader(rad), c_kolumner(kol) //User defined
Konstruktor
{
*matris = new Intvektor[c_rader]; //********* Here does the
program krasch ************
for (int i=0; i < c_rader; i++)
matris[i] = new Intvektor[c_kolumner];
}
private:
int c_rader, c_kolumner;
Intvektor **matris;
};