C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overloading assignment fails with gcc

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
jim.brown
Guest





PostPosted: Fri Jan 30, 2004 12:00 am    Post subject: Overloading assignment fails with gcc Reply with quote



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::operator=(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::operator=(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;
}
}

Back to top
Victor Bazarov
Guest





PostPosted: Fri Jan 30, 2004 12:10 am    Post subject: Re: Overloading assignment fails with gcc Reply with quote



"jim.brown" <jim.brown (AT) mindspring (DOT) com> wrote...
Quote:
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::operator=(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);

Quote:
TestCl(const TestCl& cc); // Copy constructor
TestCl getTestCl();

This function returns a temporary, which cannot be bound to a non-
const reference, see below.

Quote:
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::operator=(TestCl& right){ // assignment overload

Again, 'right' may need to be made 'const' here.

Quote:
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".

Quote:
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.

Quote:
} catch( ... ){
std::cout << "Error" << std::endl;
}
}


HTH

Victor



Back to top
Janusz Szpilewski
Guest





PostPosted: Fri Jan 30, 2004 1:11 am    Post subject: Re: Overloading assignment fails with gcc Reply with quote



jim.brown wrote:

Quote:
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::operator=(TestCL&)


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

TestCl::operator=(const TestCL&)


Regards,
Janusz


Back to top
jim.brown
Guest





PostPosted: Fri Jan 30, 2004 12:06 pm    Post subject: Re: Overloading assignment fails with gcc Reply with quote

Many thanks to posters. This was, of course, the answer.
Jim

jim.brown wrote:
Quote:
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::operator=(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::operator=(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;
}
}



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.