Home > Backend Development > C++ > body text

Why Does C Compiler Error C2280 \'attempting to reference a deleted function\' Occur in Visual Studio 2015 but Not 2013?

Patricia Arquette
Release: 2024-10-27 12:14:02
Original
742 people have browsed it

Why Does C   Compiler Error C2280

C Compiler Error C2280 "attempting to reference a deleted function" in Visual Studio 2013 and 2015

In Visual Studio 2013, the following code snippet compiles without errors:

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

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

However, the same code snippet generates an error in 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 ==========
Copy after login

This is because the C standard specifies that if a class declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted.

To fix this problem, you can explicitly provide a copy constructor and copy assignment operator:

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

This will allow you to copy-construct and copy-assign objects of class A.

The above is the detailed content of Why Does C Compiler Error C2280 \'attempting to reference a deleted function\' Occur in Visual Studio 2015 but Not 2013?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!