C++ move assignment compile time error

Good day admin

I need a cue here
the below c++ code is spoiling my coffee.

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
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;

class Demo
{
string* ptr;

public:
Demo(const string& str)
{
ptr=new string(str.content());
}    
~Demo()
{
delete ptr;
}
Demo& operator=(Demo&& x)
{
delete ptr;
ptr=x.ptr;
x.ptr=nullptr;
return *this;
}
const string& content() const
{
return *ptr;
}   
Demo operator+(const Demo& rhs)
{
return Demo(content()+rhs.content());
 }
};

int main()
{
Demo foo("My coffee ");
Demo bar=Demo("is ready!");
cout<<foo.content()<<bar.content()<<endl;
return 0;
}

Output:
 
My coffee is ready!


The above c++ code is pouring out error message.
I don't know meaning of the error message.
If I change the following code line, it silence the error message:
 
Demo& operator+(Demo&& x);

Someone help explain more about that.

Thanks.
Last edited on
perhaps you can explain what it does wrong?
I see one issue. you do not initialize your ptr to nullptr, so any and all deletes could fail. it is fine to delete nullptr, nothing happens, but if you try to delete random value ptr from uninitialized value, it will not work.

unless this is a study in pointers, I advise you find a better way. Raw pointer manipulation is best avoided.
Last edited on
The error messages from the shell.
 In constructor 'Demo::Demo(const string&)':
13:20: error: 'const string' has no member named 'content'

 In member function 'Demo Demo::operator+(const Demo&)':
32:36: error: use of deleted function 'constexpr Demo::Demo(const Demo&)'

6:7: note: 'constexpr Demo::Demo(const Demo&)' is implicitly declared as deleted because 'Demo' declares a move constructor or move assignment operator

In function 'int main()':
39:26: error: use of deleted function 'constexpr Demo::Demo(const Demo&)'
Please indent your code. Even simple code becomes unreadable when you don't have proper indentation.

std::string contains no function called "content()". Your demo class itself is what contains the function called content.
You can simply called the copy constructor
ptr = new string(str);

'constexpr Demo::Demo(const Demo&)' is implicitly declared as deleted because 'Demo' declares a move constructor or move assignment operator
This is your other error. If you want the Demo's copy constructor to work, you need to define it yourself.

https://stackoverflow.com/questions/31838748/generated-copy-and-move-operators
https://www.stroustrup.com/C++11FAQ.html#default2
Last edited on
Something like (NOT properly tried/tested):

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
#include <iostream>
#include <string>

class Demo
{
	std::string *ptr {};

public:
	Demo(const std::string& str) : ptr(new std::string(str)) {}
	~Demo() { delete ptr; }
	Demo(const Demo& d) : Demo(*(d.ptr)) {}
	Demo(Demo&& d) noexcept { swap(d); }

	Demo& operator=(Demo x) noexcept { swap(x); return *this; }
	Demo operator+(const Demo& rhs) { return Demo(content() + rhs.content()); }

	Demo& swap(Demo& d) noexcept { std::swap(d.ptr, ptr); return *this; }
	const std::string& content() const noexcept { return *ptr; }
};

int main()
{
	const Demo foo("My coffee ");
	//Demo bar = Demo("is ready!");
	const Demo bar {Demo("is ready!")};

	std::cout << foo.content() << bar.content() << '\n';
}

Topic archived. No new replies allowed.