首页  >  文章  >  后端开发  >  为什么 C 编译器错误 C2280“试图引用已删除的函数”在 Visual Studio 2015 中出现,但在 2013 中却没有?

为什么 C 编译器错误 C2280“试图引用已删除的函数”在 Visual Studio 2015 中出现,但在 2013 中却没有?

Patricia Arquette
Patricia Arquette原创
2024-10-27 12:14:02743浏览

Why Does C   Compiler Error C2280

C 编译器错误 C2280 在 Visual Studio 2013 和 2015 中“尝试引用已删除的函数”

在 Visual Studio 2013 中,以下代码代码片段编译时没有错误:

class A
{
public:
   A(){}
   A(A &&){}
};

int main(int, char*)
{
   A a;
   new A(a);
   return 0;
}

但是,相同的代码片段在 Visual Studio 2015 中会生成错误:

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

这是因为 C 标准规定,如果一个类声明了一个移动构造函数或移动赋值运算符,隐式声明的复制构造函数被定义为已删除。

要解决此问题,可以显式提供复制构造函数和复制赋值运算符:

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

这个将允许您复制构造和复制分配 A 类的对象。

以上是为什么 C 编译器错误 C2280“试图引用已删除的函数”在 Visual Studio 2015 中出现,但在 2013 中却没有?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn