Класс с динамическим массивом
Доброго времени суток. Возникла проблемка с классами!
В общем, мне нужно создать динамическую матрицу, а потом загнать ее числа в 1д массив!
Проблема в том, что просто в мейне все работает, а сделал прогу в виде класса - программа летит!
Нужна ваша помощь.))
#include "stdafx.h" //here all librarys
class Array
{
int size1;
int size2;
int **pA1;
int * pA2;
void init1()
{
for (int i=0; i < size1; i++)
for (int j=0; j < size2; j++)
pA1[i][j]=rand() % 20;
cout << "init 1" <<endl;
}
void init2()
{
int index=0;
for (int i=0; i < size1; i++)
{
for (int j=0; j < size2; j++)
{
pA2[index]=pA1[i][j];
index++;
}
}
cout << "init 2" <<endl;
}
void copy()
{
init2();
int index=0;
for (int i=0; i < size1; i++)
{
for (int j=0; j < size2; j++)
{
pA2[index]=pA1[i][j];
index++;
}
}
cout << "copy" <<endl;
}
public:
Array(int SIZE1,int SIZE2)
{
size1=SIZE1;
size2=SIZE2;
int **pA1=new int *[size1];
for(int i=0;i<size1;i++)
pA1[i]=new int[size2];
int * pA2 =new int [size2*size1];
cout << "const" <<endl;
}
void show1()
{
copy();
cout << "Your 2D array: " <<endl;
for (int i=0; i < size1; i++)
{
for (int j=0; j < size2; j++)
{
cout <<setw(3)<<pA1[i][j] <<' ';
}
cout <<endl;
}
cout << "show 1" <<endl;
}
void show2()
{
init2();
cout <<endl<<endl<< "Your combined 1D array:" <<endl;
for(int i=0; i<size2*size1; i++)
cout << pA2[i] <<' ';
cout <<endl;
cout << "show 2" <<endl;
}
void delet()
{
for(int i=0;i<size1;i++)
delete []pA1[i];
delete []pA2;
cout << "delete" <<endl;
}
};
int _tmain()
{
int size1=0,size2=0;
cout << "How many columns you want: ";
cin >>size2;
cout << "How many rows you want: ";
cin >>size1;
Array ar1(size1,size2);
ar1.show1();
ar1.show2();
return 0;
}
Источник: Stack Overflow на русском