本文へスキップ
BecomeCoder

C++のエラー · エラー6

use of deleted function ― コピーできない型をコピーした(コンパイル)

unique_ptr など「コピー禁止」の型をコピーしようとすると出ます。

出るコード

#include <memory>
int main() {
    std::unique_ptr<int> a(new int(1));
    std::unique_ptr<int> b = a;   // unique_ptr はコピー不可
}

エラーメッセージ

error: use of deleted function 'std::unique_ptr<...>::unique_ptr(const std::unique_ptr<...>&)'

直し方

所有権を移すなら std::move(a) を使います(auto b = std::move(a);)。共有したいなら shared_ptr にします。