I was just going through some old code that was in the 'should work on' folder when I came across an old code scrap that overloaded the assignment operator. Anyway, I tried to compile it, but it wouldn't work. It keeps on giving me the error:
cardsclass.hpp:109:28: error: ‘card& operator=(card&)’ must be a nonstatic member function
card & operator=(card & prm) {
^
Any ideas on what I'm doing wrong? On a side note, I tried to compile with clang version 3.4, but when I tried that, it gave me 10 errors! Here they are:
cardsclass.hpp:109:8: error: overloaded 'operator=' must be a binary operator (has 1 parameter)
card & operator=(card & prm) {
^
cardsclass.hpp:111:5: error: invalid use of 'this' outside of a non-static member function
if(this == &prm) {
^
cardsclass.hpp:113:11: error: invalid use of 'this' outside of a non-static member function
return *this;
^
cardsclass.hpp:116:9: error: use of undeclared identifier 'itsSuite'
delete itsSuite;
^
cardsclass.hpp:117:9: error: use of undeclared identifier 'itsValue'
delete itsValue;
^
cardsclass.hpp:119:2: error: use of undeclared identifier 'itsSuite'
itsSuite = new int;
^
cardsclass.hpp:120:2: error: use of undeclared identifier 'itsValue'
itsValue = new int;
^
cardsclass.hpp:122:3: error: use of undeclared identifier 'itsSuite'
*itsSuite = prm.getSuite();
^
cardsclass.hpp:123:3: error: use of undeclared identifier 'itsValue'
*itsValue = prm.getValue();
^
cardsclass.hpp:125:10: error: invalid use of 'this' outside of a non-static member function
return *this;
^
10 errors generated.
Oh wow, I just glossed over that part. Thanks! I've never seen the swap function. Does it work the same way as what I did? Are there any other things I should know while using it?
> Are there any other things I should know while using it?
No. If the objects being swapped are of your own user defined types, they must be correctly copyable and assignable.
> Does it work the same way as what I did?
No. It simply swaps the members itsSuite and itsValue of the two objects; relying on the destructor of that (passed by value, its life-time ends when the function returns) to do the clean up.