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;
}
} 3 1577
"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
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
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; } } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rim |
last post by:
Hi,
I have been thinking about how to overload the assign operation '='.
In many cases, I wanted to provide users of my packages a natural
interface to the extended built-in types I created for...
|
by: Jon Slaughter |
last post by:
I have a chain of classes(i.e., a series of classes each containing an array
of the next class). Each class has array like access.
struct Myclass1
{
vector(Myclass2) _Myclass2;
Myclass2&...
|
by: lordkain |
last post by:
is it possible to do some kind of function overloading in c? and that
the return type is different
|
by: Achim Domma |
last post by:
Hi,
I want to use Python to script some formulas in my application. The user
should be able to write something like
A = B * C
where A,B,C are instances of some wrapper classes. Overloading...
|
by: john |
last post by:
Hey,
I know we use the pointer this to obtain a class object or class
member data. I don't follow the reason for example this code. I'am
quite confused
assingment operator
const B...
|
by: sturlamolden |
last post by:
Python allows the binding behaviour to be defined for descriptors,
using the __set__ and __get__ methods. I think it would be a major
advantage if this could be generalized to any object, by...
|
by: clicwar |
last post by:
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float...
|
by: Philipp Brune |
last post by:
Hi all,
i just played around a little with the c# operator overloading features
and an idea came to my mind. You all possibly know the Nullable<T>
Datatype. Now i thought it would be a nice idea...
|
by: Wayne Shu |
last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition).
When I read chapter 11 Operator Overloading, I have two questions.
1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |