X
xuatla
Hi,
I encountered the following compile error of c++ and hope to get your help.
test2.cpp: In member function `CTest CTest:perator+=(CTest&)':
test2.cpp:79: error: no match for 'operator=' in '*this =
CTest:perator+(CTest&)((+t2))'
test2.cpp:49: error: candidates are: CTest CTest:perator=(CTest&)
make: *** [test2.o] Error 1
I don't know why in the main function, "t2 = t1" is ok, but in the
operator += body, "*this = *this+t2" is wrong. What I am thinking is
that the compiler looks for *this+t2 first, which generate a new CTest
instance, and then copy to *this. What's the mistake I made?
The code is compiled succeedly in Visual C++. but not in linux g++ (3.3.3).
And also in Visual C++,
for ( int i = 0; ... ;.... )
defines a global int i. so I can use
for ( int i = 0; ... ;.... ) { ... }
for ( i = 0; ...;......) { ... }
but in linux g++, this is wrong. "i" is only alive in the for body. I
need to define "i" again in the 2nd for body. What's the update of this
grammar? Which one is the standard c++?
Thank you very much in advance for the help.
X
-----------------------------------------------------
source code below:
#include <iostream.h>
class CTest
{
private:
int iSize;
double *dElement;
public:
CTest( const int &size = 1);
CTest( CTest &t );
~CTest();
public:
CTest operator = ( CTest &t2 );
const int size(void) { return iSize; }
double &operator[]( const int &index ) { return dElement[index]; }
public:
CTest operator + ( CTest &t2 );
CTest operator += ( CTest &t2 );
};
// constrution
CTest::CTest( const int &size )
{
iSize = size;
dElement = new double[iSize];
for ( int i = 0; i < iSize; i++ ) dElement = 0.0;
}
// copy constrution
CTest::CTest( CTest &t2 )
{
iSize = t2.size();
dElement = new double[iSize];
for ( int i = 0; i < iSize; i++ ) dElement = t2;
}
// destruction: release the allocated memory
CTest::~CTest()
{
if (dElement) delete[] dElement;
}
// copy operator
CTest CTest:perator = ( CTest &t2 )
{ // <======== line 49
if ( iSize != t2.size() ) // reallocate the memory
{
iSize = t2.size();
if (dElement) delete [] dElement;
dElement = new double[iSize];
}
for ( int i = 0; i < iSize; i++ ) dElement = t2;
return *this;
}
CTest CTest:perator + ( CTest &t2 )
{
if ( iSize != t2.size() )
{
// errorhandle
return *this; // to be changed
}
CTest sum(t2);
for ( int i = 0; i < iSize; i++ ) sum += dElement;
return sum;
}
CTest CTest:perator += ( CTest &t2 )
{
*this = *this + t2; // <===== line 79. compile error
return *this;
}
// main function
main(void)
{
int N = 10;
CTest t1(N);
for ( int i = 0; i < N; i++ ) t1 = i;
CTest t2;
t2 = t1;
for ( int i = 0; i < N; i++ ) cout << (t1+t2) << endl;
return 1;
}
I encountered the following compile error of c++ and hope to get your help.
test2.cpp: In member function `CTest CTest:perator+=(CTest&)':
test2.cpp:79: error: no match for 'operator=' in '*this =
CTest:perator+(CTest&)((+t2))'
test2.cpp:49: error: candidates are: CTest CTest:perator=(CTest&)
make: *** [test2.o] Error 1
I don't know why in the main function, "t2 = t1" is ok, but in the
operator += body, "*this = *this+t2" is wrong. What I am thinking is
that the compiler looks for *this+t2 first, which generate a new CTest
instance, and then copy to *this. What's the mistake I made?
The code is compiled succeedly in Visual C++. but not in linux g++ (3.3.3).
And also in Visual C++,
for ( int i = 0; ... ;.... )
defines a global int i. so I can use
for ( int i = 0; ... ;.... ) { ... }
for ( i = 0; ...;......) { ... }
but in linux g++, this is wrong. "i" is only alive in the for body. I
need to define "i" again in the 2nd for body. What's the update of this
grammar? Which one is the standard c++?
Thank you very much in advance for the help.
X
-----------------------------------------------------
source code below:
#include <iostream.h>
class CTest
{
private:
int iSize;
double *dElement;
public:
CTest( const int &size = 1);
CTest( CTest &t );
~CTest();
public:
CTest operator = ( CTest &t2 );
const int size(void) { return iSize; }
double &operator[]( const int &index ) { return dElement[index]; }
public:
CTest operator + ( CTest &t2 );
CTest operator += ( CTest &t2 );
};
// constrution
CTest::CTest( const int &size )
{
iSize = size;
dElement = new double[iSize];
for ( int i = 0; i < iSize; i++ ) dElement = 0.0;
}
// copy constrution
CTest::CTest( CTest &t2 )
{
iSize = t2.size();
dElement = new double[iSize];
for ( int i = 0; i < iSize; i++ ) dElement = t2;
}
// destruction: release the allocated memory
CTest::~CTest()
{
if (dElement) delete[] dElement;
}
// copy operator
CTest CTest:perator = ( CTest &t2 )
{ // <======== line 49
if ( iSize != t2.size() ) // reallocate the memory
{
iSize = t2.size();
if (dElement) delete [] dElement;
dElement = new double[iSize];
}
for ( int i = 0; i < iSize; i++ ) dElement = t2;
return *this;
}
CTest CTest:perator + ( CTest &t2 )
{
if ( iSize != t2.size() )
{
// errorhandle
return *this; // to be changed
}
CTest sum(t2);
for ( int i = 0; i < iSize; i++ ) sum += dElement;
return sum;
}
CTest CTest:perator += ( CTest &t2 )
{
*this = *this + t2; // <===== line 79. compile error
return *this;
}
// main function
main(void)
{
int N = 10;
CTest t1(N);
for ( int i = 0; i < N; i++ ) t1 = i;
CTest t2;
t2 = t1;
for ( int i = 0; i < N; i++ ) cout << (t1+t2) << endl;
return 1;
}