使用默认复制构造函数时出错:"deleted function"
Error using defaulted copy constructor: "deleted function"
我正在使用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;
^
我不知道从哪里开始诊断这个问题,因为对我来说,复制构造函数存在并且没有被删除似乎相当明显。这可能是什么原因呢?
正如juanchopanza所指出的,这是由于Factor
类中不可复制的std::unique_ptr
数据成员导致复制构造函数被静默删除。
相关文章:
- 不断"Attempting to reference a deleted function"
- C++14 遇到奇怪的"use of deleted function"错误
- 从'deleted function'获取内存错误
- 与映射和unordered_map相关的编译错误:"attempting to reference a deleted function"
- 使用互斥锁和条件变量作为成员时如何修复"use of deleted function"?
- 将 QMutex 添加到类后的"Attempting to reference a deleted function"
- 多级继承错误 C2280:"atttempting to reference a deleted function"
- 构造函数导致"use of deleted function"错误
- 错误:移动承诺时"Attempt to use a deleted function"
- C++:避免"use of deleted function"错误
- C++ 11:线程创建给我一个"Attempt to use a deleted function"错误
- C++:具有 const int 的类的 vector.erase 实例给出"attempting to reference a deleted function"错误 C2280
- "attempting to reference a deleted function"函数参数
- C++错误:"use of deleted function" minGW_32,Qt 5.7.0,Windows 10
- 错误 "Attempting to reference a deleted function" - 传回 unique_ptr 的值时
- SMFL "Cannot be Referenced, it is a deleted function"
- C++ 标准::线程"Attempt to use a deleted function"
- 合并两个unique_ptr向量时'use of deleted function'
- 尝试使用 std::bind 重新分配 std::function 并得到错误"attempting to reference a deleted function"
- 我怎么知道"C2280 - attempting to reference a deleted function"发生在哪里?