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;


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?