I’ve build class with dynamic two dimensional array, and I’ve declared constructor and copy constructor and everything work fine until when I try assign object to object. Then I receive error in debugging. Probably something with copy constructor is wrong or eventually with destructor, but I don’t know how fix it.
Please correct it or give me solution.
Thank you!
I’m using visual c++
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std ;
class twodimfield
{
public:
size_t sizeP;
string itsmali;
int itsline, itsrow, itsFragment, nslova;
char **polje;
twodimfield(int nslova, string mali);
twodimfield(const twodimfield &L);
~twodimfield();
};
twodimfield::twodimfield(int nslova, string mali)
{
itsrow = nslova+1;
itsmali = mali;
sizeP = itsmali.size();
itsline = sizeP;
itsFragment = sizeP-(nslova-1);
polje = new char * [itsline];
for(int i=0; i<itsline; i++)
{polje[i] = new char[itsrow];}
for (int m=0; m<itsline; m++)
{for (int n=0; n<itsrow; n++)
{polje[m][n] = NULL;}}
for (int i=0; i<itsFragment; i++)
{string s2;
for (int j=0; j<nslova; j++)
{s2 = s2 + mali[i+j];}
for (int l=0; l<itsrow; l++)
{polje[i][l] = s2[l];}
s2.erase();
}
}
twodimfield::twodimfield(const twodimfield &L)
{
itsline = L.itsline;
itsrow = L.itsrow;
polje = new char * [itsline];
for(int i=0; i<itsline; i++)
{polje[i] = new char[itsrow];}
for(int k=0; k<itsline; k++)
{for(int l=0; l<itsrow; l++)
{polje[k][l] = L.polje[k][l];}
}
cout<<"copy constructor"<<endl;
}
twodimfield::~twodimfield()
{
for (int i=0; i<itsline; i++)
{delete [] polje[i];}
delete [] polje;
cout<<"destructor"<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int nslova=4;
string neki;
neki = ("mariomario");
twodimfield objekt2(5,neki);
twodimfield objekt(4,neki);
objekt2 = objekt;
cout<<objekt.polje[1][1]<<endl;
system("PAUSE");
return 0;
}