错误:使用已删除的函数
我一直在研究朋友编写的一些 C++ 代码,在使用 gcc4.6 编译时出现以下我以前从未见过的错误:
error: use of deleted function
‘GameFSM_<std::array<C, 2ul> >::hdealt::hdealt()’ is implicitly deleted because the default definition would be ill-formed:
uninitialized non-static const member ‘const h_t FlopPokerGameFSM_<std::array<C, 2ul> >::hdealt::h’
编辑:这来自使用 boost MSM 的代码的一部分: Boost网页
编辑2:没有=delete()< /code> 在任何地方使用 源代码。
一般来说,这个错误是什么意思?发生此类错误时我应该寻找什么?
I've been working on some C++ code that a friend has written and I get the following error that I have never seen before when compiling with gcc4.6:
error: use of deleted function
‘GameFSM_<std::array<C, 2ul> >::hdealt::hdealt()’ is implicitly deleted because the default definition would be ill-formed:
uninitialized non-static const member ‘const h_t FlopPokerGameFSM_<std::array<C, 2ul> >::hdealt::h’
Edit: This comes from a part of the code using boost MSM: Boost Webpage
Edit2: There is no = delete()
used anywhere in the sourcecode.
Generally speaking, what does this error mean? What should I be looking for when this type of error occurs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
错误消息表明默认构造函数已被隐式删除,因为该类包含一个非静态 const 变量,该变量不会由默认构造函数 (ctor) 初始化。
由于
X::x
是const
,因此必须对其进行初始化——但默认 ctor 通常不会初始化它(因为它是 POD 类型)。因此,要获得默认构造函数,您需要自己定义一个构造函数(并且它必须初始化x
)。对于作为引用的成员,您可能会遇到同样的情况:可能值得注意的是,出于本质上相同的原因,这两种方法也将禁用赋值运算符的隐式创建。隐式赋值运算符通常执行按成员赋值,但对于 const 成员或引用成员,它不能执行此操作,因为无法对成员进行赋值。为了使赋值起作用,您需要编写自己的赋值运算符。
这就是为什么
const
成员通常应该是静态的——当您进行赋值时,您无论如何都不能分配 const 成员。在典型情况下,所有实例都将具有相同的值,因此它们也可能共享对单个变量的访问,而不是拥有多个具有相同值的变量副本。当然,创建具有不同值的实例是可能的——例如,您在创建对象时传递一个值,因此两个不同的对象可以有两个不同的值。但是,如果您尝试执行诸如交换它们之类的操作,const 成员将保留其原始值而不是被交换。
The error message indicates that the default constructor has been deleted implicitly because the class contains a non-static, const variable, which would not be initialized by the default constructor (ctor).
Since
X::x
isconst
, it must be initialized -- but a default ctor wouldn't normally initialize it (because it's a POD type). Therefore, to get a default ctor, you need to define one yourself (and it must initializex
). You can get the same kind of situation with a member that's a reference:It's probably worth noting that both of these will also disable implicit creation of an assignment operator as well, for essentially the same reason. The implicit assignment operator normally does members-wise assignment, but with a const member or reference member, it can't do that because the member can't be assigned. To make assignment work, you need to write your own assignment operator.
This is why a
const
member should typically be static -- when you do an assignment, you can't assign the const member anyway. In a typical case all your instances are going to have the same value so they might as well share access to a single variable instead of having lots of copies of a variable that will all have the same value.It is possible, of course, to create instances with different values though -- you (for example) pass a value when you create the object, so two different objects can have two different values. If, however, you try to do something like swapping them, the const member will retain its original value instead of being swapped.
您正在使用一个函数,该函数被标记为
已删除
。例如:
=delete 是 C++0x 的新功能。这意味着一旦用户使用该函数,编译器应该立即停止编译并抱怨“该函数已被删除”。
如果您看到此错误,您应该检查函数声明中的
=delete
。要了解有关 C++0x 中引入的这一新功能的更多信息,请查看此 出来。
You are using a function, which is marked as
deleted
.Eg:
The =delete is a new feature of C++0x. It means the compiler should immediately stop compiling and complain "this function is deleted" once the user use such function.
If you see this error, you should check the function declaration for
=delete
.To know more about this new feature introduced in C++0x, check this out.
当从抽象类继承并且未在子类中实现所有纯虚方法时,我遇到了此错误。
I encountered this error when inheriting from an abstract class and not implementing all of the pure virtual methods in my subclass.
gcc 4.6 支持删除函数的新功能,您可以在其中编写
禁用默认构造函数的操作。
在这里,编译器显然发现无法生成默认构造函数,并为您
=delete
删除了它。gcc 4.6 supports a new feature of deleted functions, where you can write
to disable the default constructor.
Here the compiler has obviously seen that a default constructor can not be generated, and
=delete
'd it for you.在当前的 C++0x 标准中,您可以使用删除语法显式禁用默认构造函数,例如
Gcc 4.6 是第一个支持此语法的版本,所以也许这就是问题所在......
In the current C++0x standard you can explicitly disable default constructors with the delete syntax, e.g.
Gcc 4.6 is the first version to support this syntax, so maybe that is the problem...
从 gcc 4.6 切换到 gcc 4.8 为我解决了这个问题。
Switching from gcc 4.6 to gcc 4.8 resolved this for me.
如果在初始化
std::atomic
变量时遇到此错误,如下所示:那么它无法工作,因为它使用 复制初始化,但复制构造函数被显式删除。
使用直接初始化代替:
If you get this error when initializing an
std::atomic
variable like so:Then it cannot work because this uses copy initialization but copy constructor is explicitly deleted.
Use direct initialization instead:
类中的智能指针可能会产生删除函数错误。
示例:
错误:使用已删除的函数 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&)
模板生成运算符删除状态。
因为复制操作符不知道如何复制成员m_piX。这是一个安全错误。
由开发人员决定如何处理 m_piX
3个选择:
m_piX=nullptr ,复制内容,或移动指针。
Deleted function error can be generate with smart pointer in a class.
Example:
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&)
Template generate the operator deleted state.
Because copy operator don't know how copy the member m_piX. It is a safety error.
It is to the developper to decide what to do with m_piX
3 choices:
m_piX=nullptr , copy the content, or move the pointer.