如何处理错误:在C++中使用已删除的函数‘std::unique_ptr<_Tp, _Dp>’。

huangapple go评论59阅读模式
英文:

how to deal with error: use of deleted function ‘std::unique_ptr<_Tp, _Dp> in c++

问题

remv()方法中使用find_if()后,你看到以下错误消息:

error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = H; _Dp = std::default_delete]’

请问你能解释一下我的代码有什么问题吗?我猜这与移动语义有关。

问题出在你的remv()方法中的以下行:

auto it = find_if(humans.begin(), humans.end(), [&s](unique_ptr<H> h) {return h->getName() == s;});

这个问题涉及到unique_ptr的移动语义。find_if()期望接收一个可复制的谓词,但unique_ptr是不可复制的,因为它们拥有独占的所有权。因此,你不能直接传递一个unique_ptr作为参数给find_if()的谓词。

为了解决这个问题,你可以将unique_ptr的所有权转移到find_if()的谓词中,然后在谓词内部进行比较。可以使用std::move()来完成这个操作,如下所示:

auto it = find_if(humans.begin(), humans.end(), [&s](const unique_ptr<H>& h) {return h->getName() == s;});

这里,我们将unique_ptr以常量引用的形式传递给谓词,以保留unique_ptr的所有权。然后,在谓词内部,我们可以访问unique_ptr的内容并进行比较。

这应该解决你遇到的问题。希望这能帮助你理解并修复代码中的错误。

英文:

i've got a problem with find_if() in the method remv() of HC class. This method is intended to remove a ptr from the vector humans
here is my code:

class H {
protected:
    //string name = &quot;null&quot;;
public:
    virtual string getName() = 0;
};

class P : public H {
    string name;
public:
    P() {}
    P(string n) : name(n) {}

    string getName() {return this-&gt;name;}
};

class HC {
public:
    vector&lt; unique_ptr&lt;H&gt; &gt; humans;
    void add(unique_ptr&lt;H&gt; h) { this-&gt;humans.push_back(move(h)); }
    void display() { for(auto&amp; i: humans) cout &lt;&lt; i-&gt;getName(); }
    void remv(){
        string s;
        cin &gt;&gt; s;
        auto it = find_if(humans.begin(), humans.end(), [&amp;s](unique_ptr&lt;H&gt; h) {return h-&gt;getName() == s;}); 
        humans.erase(humans.begin());
    }
};

once find_if() is put in remv() i see the following:

error: use of deleted function ‘std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Dp&gt;&amp;) [with _Tp = H; _Dp = std::default_delete]’

could you please explain me what is wrong with my code? I guess it is related with move semantics

答案1

得分: 1

unique_ptr&lt;H&gt; h - 它试图复制一个不可复制的对象。错误消息会提醒您已删除复制构造函数。如果您不想复制和修改参数,应使用const引用const unique_ptr&lt;H&gt;&amp; h

其他有用的更改。
virtual string getName() = 0; 更改为 virtual string getName() const = 0;
string getName() 更改为 string getName() const override

英文:

unique_ptr&lt;H&gt; h - it attempts to copy a not copyable object. The error message informs you about the removed copy constructor. You should use a const reference const unique_ptr&lt;H&gt;&amp; h if you don't want to copy and modify an argument.

Other useful changes.
virtual string getName() = 0; to virtual string getName() const = 0;.
string getName() to string getName() const override.

huangapple
  • 本文由 发表于 2023年6月26日 00:18:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定