Overloading the Assignment Operator

Hello everyone!

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) {
                            ^

Here's the code for the assignment operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
card & operator=(card & prm) {
	
	if(this == &prm) {
		return *this;
	}
	
	delete itsSuite;
	delete itsValue;
		
	itsSuite = new int;
	itsValue = new int;
		
	*itsSuite = prm.getSuite();
	*itsValue = prm.getValue();
		
	return *this;

}


And here is the class definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class card {
	
	public:
	
		//Constructor/Destructor
		card();
		card(int nSuite, int nValue);
		card(const card & prm);
		~card();
	
		//Operator Overloads
		card & operator=(card & prm);
		
		//Accessor Methods
		int getSuite() const {return *itsSuite;}
		int getValue() const {return *itsValue;}
		card getCard() const {return *this;}
		void getName() const;
		
		void setSuite(int nSuite) {*itsSuite = nSuite;}
		void setValue(int nValue) {*itsValue = nValue;}
	
	private:
		
		//Member Variables
		int *itsSuite;
		int *itsValue;
};


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.


Thanks in advance for the help!
The overloaded assignment operator is a member function.
1
2
// card & operator=(card & prm) { 
card & card::operator=(card & prm) {


Favour the canonical form of the overloaded assignment operator:
1
2
3
4
5
6
7
card & card::operator=( card that ) { // *** passed by value
	
        std::swap( itsSuite, that.itsSuite ) ;
        std::swap( itsValue, that.itsValue ) ;	

	return *this;
}
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?
> I've never seen the swap function.

http://en.cppreference.com/w/cpp/algorithm/swap


> 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.
Thanks!
Topic archived. No new replies allowed.