By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

Overloading assignment fails with gcc

P: n/a
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;
}
}

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"jim.brown" <ji*******@mindspring.com> wrote...
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);
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::operator=(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
Jul 22 '05 #2

P: n/a
jim.brown wrote:
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

Jul 22 '05 #3

P: n/a
Many thanks to posters. This was, of course, the answer.
Jim

jim.brown wrote:
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;
}
}


Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.