6

g++ 5.1.0 を使用して、次の C++14 プログラムをコンパイルしていますtest.cpp

#include <memory>

class Factor {
  public:
    Factor(const Factor&) = default;
    Factor(Factor&&) = default;
    Factor& operator=(const Factor&) = default;
    Factor& operator=(Factor&&) = default;
    Factor(int data) { 
      _data = std::make_unique<int>(data);
    }
    int* data() const { return _data.get(); }
  private:
    std::unique_ptr<int> _data;
};

class Node {
  public:
    Node(const Node& other) : _factor(other._factor) {
    }
  private:
    Factor _factor;
};

int main(int argc, char **argv) {
}

コンパイルしようとすると、次のエラーが発生します。

test.cpp: In copy constructor ‘Node::Node(const Node&)’:
test.cpp:19:52: error: use of deleted function ‘Factor::Factor(const Factor&)’
     Node(const Node& other) : _factor(other._factor) {
                                                    ^
test.cpp:5:5: note: ‘Factor::Factor(const Factor&)’ is implicitly deleted because the default definition would be ill-formed:
     Factor(const Factor&) = default;
     ^
test.cpp:5:5: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]’
In file included from /usr/include/c++/5/memory:81:0,
                 from test.cpp:1:
/usr/include/c++/5/bits/unique_ptr.h:356:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^

コピー コンストラクターが存在し、削除されていないことは明らかであるため、この問題の診断をどこから開始すればよいかわかりません。これの原因は何ですか?

4

1 に答える 1

7

juanchopanzaが示したように、これは私のクラスのコピー不可能なstd::unique_ptrデータ メンバーFactorが原因であり、その結果、コピー コンストラクターがサイレントに削除されました。

于 2015-10-04T22:23:24.043 に答える