首頁  >  文章  >  後端開發  >  為什麼 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