Home > Backend Development > C++ > body text

Why Am I Getting Compiler Error C2280 \'Attempting to Reference a Deleted Function\' in Visual Studio 2015?

DDD
Release: 2024-10-26 17:03:02
Original
846 people have browsed it

Why Am I Getting Compiler Error C2280

Compiler Error C2280 "Attempting to Reference a Deleted Function" in Visual Studio 2015

The Visual Studio 2015 compiler, unlike its 2013 predecessor, automatically generates a deleted copy constructor for classes defining a move constructor or move assignment operator. This behavior is mandated by the C standard to prevent accidental copying in situations where moving is preferred.

In your code snippet, the class A has a move constructor A(A &&), which in turn implies a deleted copy constructor as per the standard. Consequently, the new A(a) expression in main triggers error C2280.

To resolve this issue, you can explicitly declare the copy constructor in A:

<code class="cpp">class A {
public:
   A() {}
   A(A &&) {}
   A(const A&) = default; // Explicitly declare the copy constructor as defaulted
};</code>
Copy after login

Alternatively, if you genuinely intend to disable copying and only allow moving, you can declare the copy constructor and copy assignment operator as deleted:

<code class="cpp">class A {
public:
   A() {}
   A(A &&) {}
   A(const A&) = delete;
   A& operator=(const A&) = delete; // Delete copy assignment operator
};</code>
Copy after login

Remember, if you choose to disable copying, you will also need to declare a move assignment operator and destructor to comply with the Rule of Five.

The above is the detailed content of Why Am I Getting Compiler Error C2280 \'Attempting to Reference a Deleted Function\' in Visual Studio 2015?. 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
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!