#include <iostream>
struct qwer
{
const int x{1};
};
qwer tt()
{
qwer xx;
return xx;
}
int main()
{
qwer qq;
qq = tt();
return 0;
}
给出错误消息:
qwer.cpp: In function ‘int main()’:
qwer.cpp:18:10: error: use of deleted function ‘qwer& qwer::operator=(qwer&&)’
18 | qq = tt();
| ^
qwer.cpp:4:8: note: ‘qwer& qwer::operator=(qwer&&)’ is implicitly deleted because the default definition would be ill-formed:
4 | struct qwer
| ^~~~
qwer.cpp:4:8: error: non-static const member ‘const int qwer::x’, can’t use default assignment operator
请解释是什么问题
默认分配只执行字段的成员分配。
但是您的字段(成员)被声明为
const,即 不能分配给它。编译器用自己的语言表达了这一点:)