-
December 29th, 2008, 07:52 PM
#1
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.
int main()
{
myClass A;
A=myFunction();
}
or
myClass anotherFunction()
{
return myFunction();
}
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.
Why does this happen and how can I fix it?
Thanks!
-
December 29th, 2008, 08:35 PM
#2
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,2010
In theory, there is no difference between theory and practice; 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
-
December 29th, 2008, 08:46 PM
#3
Re: Functions returning my user-defined class (w/pointer) don't like operator=.
Originally Posted by oliveman
Why does this happen and how can I fix it?
My crystal ball says that you have a bug on line 57.
Seriously, how can we fix an error if we don't see the class that's causing the problem?
Regards,
Paul McKenzie
-
December 29th, 2008, 09:05 PM
#4
Re: Functions returning my user-defined class (w/pointer) don't like operator=.
Originally Posted by Paul McKenzie
My crystal ball says that you have a bug on line 57.
Seriously, how can we fix an error if we don't see the class that's causing the problem?
Regards,
Paul McKenzie
A bug in 57 would be quite upsetting
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; 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
-
December 29th, 2008, 10:24 PM
#5
Re: Functions returning my user-defined class (w/pointer) don't like operator=.
Here is a class which produces the errors. With this code:
Code:
class myClass
{
public:
myClass();
int Read(int k);
myClass(myClass & original);
myClass & operator=(myClass & original);
~myClass();
friend myClass myFunction();
friend myClass anotherFunction();
private:
int * ptr;
};
int myClass::Read(int k)
{
return ptr[k];
}
myClass::myClass()
{
ptr=new int[5];
}
myClass::myClass(myClass & original)
{
int k=0;
while (k<5)
{
ptr[k]=original.Read(k);
k++;
}
}
myClass & myClass::operator=(myClass & original)
{
int k=0;
while (k<5)
{
ptr[k]=original.Read(k);
k++;
}
return *this;
}
myClass::~myClass()
{
delete [] ptr;
}
myClass myFunction()
{
myClass A;
return A;
}
myClass anotherFunction()
{
return myFunction();
}
int main()
{
myClass A;
A=myFunction();
A=anotherFunction();
}
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&)
-
December 29th, 2008, 10:41 PM
#6
Re: Functions returning my user-defined class (w/pointer) don't like operator=.
You are attempting to take a non-const reference of a temportary.
Code:
int Read(int k) const;
myClass(myClass const & original);
const myClass & operator=(myClass const & original);
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,2010
In theory, there is no difference between theory and practice; 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
-
December 29th, 2008, 11:12 PM
#7
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.
Code:
myClass::myClass() : ptr(new int[5])
{ }
myClass::myClass(const myClass & original) : ptr(new int[5])
{
k = 0;
while (k<5)
{
ptr[k]=original.Read(k);
k++;
}
}
Regards,
Paul McKenzie
-
December 29th, 2008, 11:15 PM
#8
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,2010
In theory, there is no difference between theory and practice; 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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|