Functions returning my user-defined class (w/pointer) don't like operator=.
I have a user-defined class containing several integers and a pointer (to an array of values in another user-defined class which does not contain a pointer, and which works fine and has no similar problems) as data elements. I am using g++.
My problem is this:
If I take a function returning my class, e.g.
myClass myFunction()
and attempt to either set a variable equal to it or return it, e.g.
I get a compiler error from operator=/the copy constructor, respectively. I have overloaded both of these.
The error is that it seems to call myClass(myClass) instead of myClass(myClass&) for the copy constructor and operator=(myClass) instead of operator=(myClass&), and obviously it doesn't like that. I assume the problem is that things are stored at a particular address only temporarily (until the function finishes), but I could easily be wrong.
Re: Functions returning my user-defined class (w/pointer) don't like operator=.
1) Please use code tags. Go back and re-read the "BEFORE you post announcement if you have forgotten how.
2) Please post minimal yet complete code. This means the smallest possible sample that someone can copy/paste into an empty file and run through the compiler to reproduce the exact issue (error line number should line up!
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
I get these compiler errors:
./file.cpp: In function ‘myClass anotherFunction()’:
./file.cpp:52: error: no matching function for call to ‘myClass::myClass(myClass)’
./file.cpp:22: note: candidates are: myClass::myClass(myClass&)
./file.cpp: In function ‘int main()’:
./file.cpp:57: error: no match for ‘operator=’ in ‘A = myFunction()()’
./file.cpp:31: note: candidates are: myClass& myClass:perator=(myClass&)
./file.cpp:58: error: no match for ‘operator=’ in ‘A = anotherFunction()()’
./file.cpp:31: note: candidates are: myClass& myClass:perator=(myClass&)
You need to do some detailed studying on what "const correctness" means....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Re: Functions returning my user-defined class (w/pointer) don't like operator=.
Originally Posted by oliveman
Here is a class which produces the errors.
In addition to what CPUWizard points out, your code has other runtime errors. Also, you need to format your code a little better, since too much of it is not indented properly and is hard to read.
First, usage of std::vector eliminates the need for copy constructor and assignment operator. However, here is your mistake:
Code:
myClass::myClass(const myClass & original)
{
int k=0;
while (k<5)
{
ptr[k]=original.Read(k); // Error! Invalid memory access
k++;
}
}
See the line in red? It is wrong, since ptr was not allocated.
The fix to both the default and copy constructor is to make sure that ptr is allocated on initialization of the object. You would use the initializer list instead of allocating it within the constructor body.
Re: Functions returning my user-defined class (w/pointer) don't like operator=.
Paul,
I only made sure it would compile, not that it was logically correct
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!) 2008, 2009 In theory, there is no difference between theory and paractice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Bookmarks