明确默认的副本/移动分配运算符隐式删除,因为字段没有复制/移动操作员。 C++
我是C ++的新手,不知道为什么会发生这种情况以及如何解决。这是代码的一些片段:
标题文件:
class Dictionary{
private:
string filename;
const string theSeparators;
public:
Dictionary(const string& filename, const string& separators = "\t\n");
Dictionary() = delete;
~Dictionary() = default;
Dictionary(const Dictionary&) = default;
Dictionary(Dictionary&&) = default;
Dictionary& operator=(const Dictionary&) = default;
Dictionary& operator=(Dictionary&&) = default;
};
CPP文件:
Dictionary::Dictionary(const string& filename, const string& separators = "\t\n"){
/* some stuff for the filename*/
}
错误:复制“字典”的分配运算符被隐式删除,因为字段“ theseparators”没有复制分配运算符
const string theseparators;
错误:“词典”的移动分配操作员被隐式删除,因为字段“ eparators”没有移动分配操作员
const string theseparators;
I'm new to C++ and dont know why this is happening and how to fix it. Here's some snippets of the code:
header file:
class Dictionary{
private:
string filename;
const string theSeparators;
public:
Dictionary(const string& filename, const string& separators = "\t\n");
Dictionary() = delete;
~Dictionary() = default;
Dictionary(const Dictionary&) = default;
Dictionary(Dictionary&&) = default;
Dictionary& operator=(const Dictionary&) = default;
Dictionary& operator=(Dictionary&&) = default;
};
cpp file:
Dictionary::Dictionary(const string& filename, const string& separators = "\t\n"){
/* some stuff for the filename*/
}
error: copy assignment operator of 'Dictionary' is implicitly deleted because field 'theSeparators' has no copy assignment operator
const string theSeparators;
error:move assignment operator of 'Dictionary' is implicitly deleted because field 'theSeparators' has no move assignment operator
const string theSeparators;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该数据成员
用限制符
const
定义。因此,它在构造函数中初始化后不能重新分配。因此,编译器将副本和移动分配运算符定义为已删除。
您只需删除此数据成员的预选赛
const
即可。或者,如果要保留数据成员的预选赛
const
,则必须在例如构造函数中使用MEM-INIALIDENIDERS列表,但在这种情况下,将仍然删除复制和移动分配运算符。
This data member
is defined with the qualifier
const
. So it can not be reassigned after its initialization in a constructor.Thus the compiler defined the copy and move assignment operators as deleted.
You can just remove the qualifier
const
for this data member.Or if to keep the qualifier
const
for the data member then you have to use mem-initialing lists in constructors like for exampleBut in this case the copy and move assignment operators will be still deleted.