<bdo id='JSpPi'></bdo><ul id='JSpPi'></ul>
  • <small id='JSpPi'></small><noframes id='JSpPi'>

    1. <legend id='JSpPi'><style id='JSpPi'><dir id='JSpPi'><q id='JSpPi'></q></dir></style></legend>

    2. <i id='JSpPi'><tr id='JSpPi'><dt id='JSpPi'><q id='JSpPi'><span id='JSpPi'><b id='JSpPi'><form id='JSpPi'><ins id='JSpPi'></ins><ul id='JSpPi'></ul><sub id='JSpPi'></sub></form><legend id='JSpPi'></legend><bdo id='JSpPi'><pre id='JSpPi'><center id='JSpPi'></center></pre></bdo></b><th id='JSpPi'></th></span></q></dt></tr></i><div id='JSpPi'><tfoot id='JSpPi'></tfoot><dl id='JSpPi'><fieldset id='JSpPi'></fieldset></dl></div>
    3. <tfoot id='JSpPi'></tfoot>

        C++ 编译器错误 C2280“试图引用已删除的函数"在 Visual Studio 2013 和 2015

        时间:2023-06-03

          <bdo id='C4jXW'></bdo><ul id='C4jXW'></ul>

              <tfoot id='C4jXW'></tfoot>

                <i id='C4jXW'><tr id='C4jXW'><dt id='C4jXW'><q id='C4jXW'><span id='C4jXW'><b id='C4jXW'><form id='C4jXW'><ins id='C4jXW'></ins><ul id='C4jXW'></ul><sub id='C4jXW'></sub></form><legend id='C4jXW'></legend><bdo id='C4jXW'><pre id='C4jXW'><center id='C4jXW'></center></pre></bdo></b><th id='C4jXW'></th></span></q></dt></tr></i><div id='C4jXW'><tfoot id='C4jXW'></tfoot><dl id='C4jXW'><fieldset id='C4jXW'></fieldset></dl></div>
                    <tbody id='C4jXW'></tbody>
                1. <small id='C4jXW'></small><noframes id='C4jXW'>

                  <legend id='C4jXW'><style id='C4jXW'><dir id='C4jXW'><q id='C4jXW'></q></dir></style></legend>
                  本文介绍了C++ 编译器错误 C2280“试图引用已删除的函数"在 Visual Studio 2013 和 2015 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  此代码段在 Visual Studio 2013(版本 12.0.31101.00 更新 4)中编译没有错误

                  This snippet is compiled without errors in Visual Studio 2013 (Version 12.0.31101.00 Update 4)

                  class A
                  {
                  public:
                     A(){}
                     A(A &&){}
                  };
                  
                  int main(int, char*)
                  {
                     A a;
                     new A(a);
                     return 0;
                  }
                  

                  在 Visual Studio 2015 RC(版本 14.0.22823.1 D14REL)中编译时出现此错误:

                  while it is compiled with this error in Visual Studio 2015 RC (Version 14.0.22823.1 D14REL):

                  1>------ Build started: Project: foo, Configuration: Debug Win32 ------
                  1>  foo.cpp
                  1>c:devfoofoo.cpp(11): error C2280: 'A::A(const A &)': attempting to reference a deleted function
                  1>  c:devfoofoo.cpp(6): note: compiler has generated 'A::A' here
                  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
                  

                  我认为 Visual Studio 2015 附带的编译器生成复制构造函数并将其标记为 =delete,因此我收到错误 C2280(顺便说一下,我在 msdn.microsoft.com).

                  I think that the compiler shipped with Visual Studio 2015 generates the Copy Constructor and marks it as =delete and so I get the error C2280 (which, by the way, I cannot find documented on msdn.microsoft.com).

                  现在,假设我有一个可以用 Visual Studio 2013 编译的代码库(它可以工作,因为它依赖于编译器自动生成的代码)但由于 C2280 而不能用 Visual Studio 2015 编译,我该如何修复问题?

                  Now, let's say I have a codebase which is compilable with Visual Studio 2013 (and it works because it relies on the code generated automatically by the compiler) but not compilable with Visual Studio 2015 due to C2280, how can I fix the problem?

                  我想以这种方式声明 A 类:

                  I was thinking to declare class A in this way:

                  class A
                  {
                  public:
                     A(){}
                     A(A &&){}
                     A(const A&)=default;
                  };
                  

                  我错过了什么吗?

                  推荐答案

                  来自 [class.copy]/7,强调我的:

                  From [class.copy]/7, emphasis mine:

                  如果类定义未显式声明复制构造函数,则隐式声明非显式.如果类定义声明了移动构造函数或移动赋值运算符,则隐式声明的副本构造函数定义为删除;否则,它被定义为默认(8.4).后一种情况被弃用,如果该类具有用户声明的复制赋值运算符或用户声明的析构函数.

                  If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

                  在第 18 段中有一个具有相似措辞的副本分配的等效部分.所以你的班级真的是:

                  There is an equivalent section with similar wording for copy assignment in paragraph 18. So your class is really:

                  class A
                  {
                  public:
                     // explicit
                     A(){}
                     A(A &&){}
                  
                     // implicit
                     A(const A&) = delete;
                     A& operator=(const A&) = delete;
                  };
                  

                  这就是你不能复制构造它的原因.如果您提供移动构造函数/赋值,并且您仍然希望该类是可复制的,则必须明确提供那些特殊的成员函数:

                  which is why you can't copy-construct it. If you provide a move constructor/assignment, and you still want the class to be copyable, you will have to explicitly provide those special member functions:

                      A(const A&) = default;
                      A& operator=(const A&) = default;
                  

                  您还需要声明一个移动赋值运算符.如果您真的需要这些特殊功能,您可能还需要析构函数.请参阅五分法则.

                  You will also need to declare a move assignment operator. If you really have a need for these special functions, you will also probably need the destructor. See Rule of Five.

                  这篇关于C++ 编译器错误 C2280“试图引用已删除的函数"在 Visual Studio 2013 和 2015 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:值初始化和非 POD 类型 下一篇:在具有多个接口的对象中实现 QueryInterface() 时,为什么我需要显式向上转换()

                  相关文章

                      <bdo id='c2PKh'></bdo><ul id='c2PKh'></ul>

                    <small id='c2PKh'></small><noframes id='c2PKh'>

                    <legend id='c2PKh'><style id='c2PKh'><dir id='c2PKh'><q id='c2PKh'></q></dir></style></legend>
                  1. <tfoot id='c2PKh'></tfoot>
                    <i id='c2PKh'><tr id='c2PKh'><dt id='c2PKh'><q id='c2PKh'><span id='c2PKh'><b id='c2PKh'><form id='c2PKh'><ins id='c2PKh'></ins><ul id='c2PKh'></ul><sub id='c2PKh'></sub></form><legend id='c2PKh'></legend><bdo id='c2PKh'><pre id='c2PKh'><center id='c2PKh'></center></pre></bdo></b><th id='c2PKh'></th></span></q></dt></tr></i><div id='c2PKh'><tfoot id='c2PKh'></tfoot><dl id='c2PKh'><fieldset id='c2PKh'></fieldset></dl></div>