C++ 错误:使用已删除的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5966698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: use of deleted function
提问by shuttle87
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:
我一直在研究一个朋友写的一些 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'
Edit: This comes from a part of the code using boost MSM: Boost Webpage
编辑:这来自使用 boost MSM 的代码的一部分:Boost Webpage
Edit2: There is no = delete()
used anywhere in the sourcecode.
Edit2:= delete()
源代码中的任何地方都没有使用。
Generally speaking, what does this error mean? What should I be looking for when this type of error occurs?
一般来说,这个错误是什么意思?发生此类错误时,我应该寻找什么?
回答by Jerry Coffin
The error message clearly says that the default constructor has been deleted implicitly. It even says why: the class contains a non-static, const variable, which would not be initialized by the default ctor.
错误消息清楚地表明默认构造函数已被隐式删除。它甚至说明了原因:该类包含一个非静态的 const 变量,该变量不会被默认构造函数初始化。
class X {
const int x;
};
Since X::x
is const
, 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 initialize x
). You can get the same kind of situation with a member that's a reference:
由于X::x
is const
,它必须被初始化——但是默认的构造函数通常不会初始化它(因为它是一个 POD 类型)。因此,要获得默认的 ctor,您需要自己定义一个(并且它必须 initialize x
)。您可以使用作为参考的成员获得相同类型的情况:
class X {
whatever &x;
};
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.
可能值得注意的是,出于基本相同的原因,这两个也将禁用赋值运算符的隐式创建。隐式赋值运算符通常会按成员进行赋值,但是对于 const 成员或引用成员,它不能这样做,因为该成员不能被赋值。要使赋值起作用,您需要编写自己的赋值运算符。
This is why a const
member should typicallybe 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.
这就是为什么const
成员通常应该是静态的——当您进行赋值时,无论如何都不能分配 const 成员。在典型的情况下,您的所有实例都将具有相同的值,因此它们最好共享对单个变量的访问权限,而不是拥有多个都具有相同值的变量副本。
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.
当然,创建具有不同值的实例是可能的——您(例如)在创建对象时传递一个值,因此两个不同的对象可以有两个不同的值。但是,如果您尝试执行诸如交换它们之类的操作,则 const 成员将保留其原始值而不是被交换。
回答by Alok Save
You are using a function, which is marked as deleted
.
Eg:
您正在使用标记为 的函数deleted
。
例如:
int doSomething( int ) = delete;
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.
=delete 是 C++0x 的一个新特性。这意味着一旦用户使用该函数,编译器应立即停止编译并抱怨“该函数已被删除”。
If you see this error, you should check the function declaration for =delete
.
如果您看到此错误,则应检查=delete
.
To know more about this new feature introduced in C++0x, check thisout.
要了解有关 C++0x 中引入的这项新功能的更多信息,请查看此内容。
回答by Bo Persson
gcc 4.6 supports a new feature of deleted functions, where you can write
gcc 4.6 支持删除函数的新特性,你可以在这里写
hdealt() = delete;
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.
这里编译器显然已经看到无法生成默认构造函数,并=delete
为您提供了它。
回答by Christopher
I encountered this error when inheriting from an abstract class and not implementing all of the pure virtual methods in my subclass.
我在从抽象类继承并且没有在我的子类中实现所有纯虚方法时遇到了这个错误。
回答by jarmond
In the current C++0x standard you can explicitly disable default constructors with the delete syntax, e.g.
在当前的 C++0x 标准中,您可以使用删除语法显式禁用默认构造函数,例如
MyClass() = delete;
Gcc 4.6 is the first version to support this syntax, so maybe that is the problem...
Gcc 4.6 是第一个支持这种语法的版本,所以也许这就是问题所在......
回答by Michael Bosworth
Switching from gcc 4.6 to gcc 4.8 resolved this for me.
从 gcc 4.6 切换到 gcc 4.8 为我解决了这个问题。