"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Am I Getting Compiler Error C2280 \"Attempting to Reference a Deleted Function\" in Visual Studio 2015?

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

Published on 2024-11-05
Browse:629

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:

class A {
public:
   A() {}
   A(A &&) {}
   A(const A&) = default; // Explicitly declare the copy constructor as defaulted
};

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

class A {
public:
   A() {}
   A(A &&) {}
   A(const A&) = delete;
   A& operator=(const A&) = delete; // Delete copy assignment operator
};

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3