明确默认的副本/移动分配运算符隐式删除,因为字段没有复制/移动操作员。 C++

发布于 2025-02-08 17:49:07 字数 1037 浏览 2 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2025-02-15 17:49:07

该数据成员

 const string theSeparators; 

用限制符const定义。因此,它在构造函数中初始化后不能重新分配。

因此,编译器将副本和移动分配运算符定义为已删除。

您只需删除此数据成员的预选赛const即可。

或者,如果要保留数据成员的预选赛const,则必须在例如构造函数中使用MEM-INIALIDENIDERS列表,

Dictionary::Dictionary(const string& filename, const string& separators = "\t\n")
    : filename( filename ), theSeparators( separators )
{
    //...
}

但在这种情况下,将仍然删除复制和移动分配运算符。

This data member

 const string theSeparators; 

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 example

Dictionary::Dictionary(const string& filename, const string& separators = "\t\n")
    : filename( filename ), theSeparators( separators )
{
    //...
}

But in this case the copy and move assignment operators will be still deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文