A moving assignment exists with a friend function in the class and error occurs.

When I declare a moving assignment and a friend function in the class, an error occurs at "return temp" on compilation. I donot know why.
Any reply would be appreciated.
Thanks.

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
29
30
31
32
33
#include <iostream>
#include<string>
using namespace std;

class rectangle
{

	int width, height;
public:
	rectangle(int& a, int& b) :width(a), height(b) {}
	rectangle() {}
	int area() const { return width * height; }

	rectangle& operator=(rectangle&& e)
	{
		width = e.width;
		height = e.height;
		return *this;
	}

	friend rectangle duplicate(const rectangle&);


};
rectangle duplicate(const rectangle& cc)
{
	rectangle temp;
	temp.width = cc.width * 2;
	temp.height = cc.height * 2;
	return temp;

}
The implicit copy constructor has been removed as a non-default constructor has been provided.

As public, add

 
rectangle(const rectangle&) = default;



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class rectangle
{
	int width {}, height {};

public:
	rectangle(int a, int b) : width(a), height(b) {}
	rectangle() {}
	rectangle(const rectangle&) = default;

	int area() const { return width * height; }

	rectangle& operator=(rectangle&& e) {
		width = e.width;
		height = e.height;
		return *this;
	}

	friend rectangle duplicate(const rectangle&);
};

rectangle duplicate(const rectangle& cc)
{
	return rectangle(cc.width, cc.height);
}

Last edited on
@seeplus
Thanks.
I got it now.
When the moving assignment is defined, the default coyping constructor and copying assignment is removed.

And, "return *this;" in the moving operator actually calls the copying constructor or assignment which doesnot exist, so an error occurs on compilation.
Topic archived. No new replies allowed.