Overloading assignment fails with gcc

J

jim.brown

The attached code implements a test class to show an error in
overloading operator=. This code works on Windows with Visual
Studio and simpler cases work with gcc 3.3.2 on Solaris 9.
On Windows, operator= gets called twice and I'm not sure whay
that is.

This fails on Solaris with:

In function 'int main(int, char**)'
error: no match for 'operator=' in 't2 = TestCL::getTestCL()()'
error: candidates are : TestCL& TestCl::eek:perator=(TestCL&)

I have seen this message when there is something ambiguous and
more than one candidate are listed. Since my code runs on Windows
it has to be something subtle about some default function which
gcc doesn't do or something.

Does anyone see something I did wrong?

Jim
_______________________________________________________________

long globalint = 0; // global for TestCl
class TestCl{
public:
TestCl(); // Constructor
~TestCl(); // Descructor
TestCl& operator=(TestCl& right); // assignment overload
TestCl(const TestCl& cc); // Copy constructor
TestCl getTestCl();
long data;
}; // end TestCl

TestCl::TestCl(){ // Constructor
data = ++globalint;
}
TestCl::~TestCl(){ // Descructor
data = (-data);
}
TestCl::TestCl(const TestCl& cj){ // Copy constructor
data = 100*(++globalint);
}
TestCl& TestCl::eek:perator=(TestCl& right){ // assignment overload
if (this != &right){
data = right.data+7;
}
return *this;
}

TestCl TestCl::getTestCl(){
TestCl res;
return res;
}

int main (int argc, char **argv){
try{
TestCl t1;
TestCl t2 = t1.getTestCl();
t2 = t1.getTestCl();
} catch( ... ){
std::cout << "Error" << std::endl;
}
}
 
V

Victor Bazarov

jim.brown said:
The attached code implements a test class to show an error in
overloading operator=. This code works on Windows with Visual
Studio and simpler cases work with gcc 3.3.2 on Solaris 9.
On Windows, operator= gets called twice and I'm not sure whay
that is.

This fails on Solaris with:

In function 'int main(int, char**)'
error: no match for 'operator=' in 't2 = TestCL::getTestCL()()'
error: candidates are : TestCL& TestCl::eek:perator=(TestCL&)

I have seen this message when there is something ambiguous and
more than one candidate are listed. Since my code runs on Windows
it has to be something subtle about some default function which
gcc doesn't do or something.

Does anyone see something I did wrong?

Jim
_______________________________________________________________

long globalint = 0; // global for TestCl
class TestCl{
public:
TestCl(); // Constructor
~TestCl(); // Descructor
TestCl& operator=(TestCl& right); // assignment overload

The problem with this is that 'right' is non-const. You might want
to rethink your requirement and perhaps do

TestCl& operator=(TestCl const &right);
TestCl(const TestCl& cc); // Copy constructor
TestCl getTestCl();

This function returns a temporary, which cannot be bound to a non-
const reference, see below.
long data;
}; // end TestCl

TestCl::TestCl(){ // Constructor
data = ++globalint;
}
TestCl::~TestCl(){ // Descructor
data = (-data);
}
TestCl::TestCl(const TestCl& cj){ // Copy constructor
data = 100*(++globalint);
}
TestCl& TestCl::eek:perator=(TestCl& right){ // assignment overload

Again, 'right' may need to be made 'const' here.
if (this != &right){
data = right.data+7;
}
return *this;
}

TestCl TestCl::getTestCl(){
TestCl res;
return res;
}

int main (int argc, char **argv){
try{
TestCl t1;
TestCl t2 = t1.getTestCl();

The statement above has no _assignment_ in it (although there is
the '=' sign). It's what is known as "copy-initialisation".
t2 = t1.getTestCl();

Here is the trouble. 'getTestCl()' returns a temporary object to
which a non-const reference needed for the assignment operator
cannot be bound.
} catch( ... ){
std::cout << "Error" << std::endl;
}
}

HTH

Victor
 
J

Janusz Szpilewski

jim.brown said:
This fails on Solaris with:

In function 'int main(int, char**)'
error: no match for 'operator=' in 't2 = TestCL::getTestCL()()'
error: candidates are : TestCL& TestCl::eek:perator=(TestCL&)

t1.getTestCl() returns a temporary so it cannot be assigned to a
non-const reference. Hence make the reference parameter constant:

TestCl::eek:perator=(const TestCL&)


Regards,
Janusz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top