why the compiler say can not overloa an operator

in the definitions class I have .h file

TwoD operator =( const TwoD &r )
{
return ( *this = r);
}

IN THE PRIVATE SETION I HAVE A VARIABLE DIFINE
int ** eletwoD;
int x,y;

In .cpp file specifications file I have


TwoD& TwoD::operator =( const TwoD &r ) // overload assigment operator
{

if (x != r.x && y != r.y)
{
for(int i = 0; i < x; i++)
delete[] elewoD[i];

delete[eletwoD];

eletwoD = new int *[x]; // dynamically allocated array of int rows length

for( int i = 0; i < x+1; i++)
{
eletwoD[i] = new int[y]; // Fill the row up until max columns
}

}

x = r.x;
y = r.y;

for(int i = 0; i < x+1; i++)
{
eletwoD[i] = new int[x]; // Fill the row up til max columns
}

for ( int i = 0; i < x+ 1; i++ )
{
for ( int j = 0; j < y; j++ )
{
eletwoD[i][j] = r.eletwoD[i][j];
}
}

return *this;
}

Does this even compile? It looks like you are overloading the assignment operator on return type only (which is illegal), and one of the instances looks infinitely recursive..
Please use [code][/code] tags.

Remove the body of operator = in the header. ie: TwoD &operator =( const TwoD &r );
one of the instances looks is infinitely recursive..


Fixed...Remove the top one, it does nothing except cause a stack overflow.
Topic archived. No new replies allowed.