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;
}

1. Main problem is that you also need to define operator=.
2. you can simplify other parts. '
3. You need better formatting style. to make the code easier to read.

#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);
	void operator=(const twodimfield& t);
	~twodimfield();
};


twodimfield::twodimfield(int nslova, string mali)
{
	int i;
	itsrow = nslova+1;
	itsmali = mali;
	sizeP = itsmali.size();
	itsline = sizeP;
	itsFragment = sizeP-(nslova-1);
	polje = new char * [itsline];
	for(i=0; i<itsline; i++)
	{
		polje[i] = new char[itsrow];
		memset(polje[i],0,itsrow);
	}

	for (i=0; i < itsFragment; i++)
	{
		string s2;
		for (int j=0; j<nslova; j++)
		{
			s2 += mali[i+j];
		}
		for (int l=0; l<itsrow; l++)
		{
			polje[i][l] = s2[l];
		}
	}

}


twodimfield::twodimfield(const twodimfield &L)
{
	operator=(L);
}

void twodimfield::operator=(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];
		memcpy(polje[i],L.polje[i],itsrow);
	} 

	cout<<"copy constructor"<<endl;
}



twodimfield::~twodimfield()
{
	for (int i=0; i<itsline; i++)
	{
		delete [] polje[i];
	}
	delete [] polje;
	cout<<"destructor"<<endl;
}

int main()
{	
	int nslova=4;	
	string neki("mariomario");
	twodimfield objekt2(5,neki);
	twodimfield objekt(4,neki);
	objekt2 = objekt;
	cout<<objekt.polje[1][1]<<endl;

	
	system("PAUSE");
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.