整理 Visual C++ 開發中高頻出現的 20 個錯誤/警告代碼,附帶中文說明與常見成因,助您快速定位並修正問題。
| 代碼 | 訊息 | 中文說明與常見原因 |
|---|---|---|
| C1010 | unexpected end of file while looking for precompiled header directive | 尋找預編譯標頭時遇到檔案結尾。通常是忘記 #include "stdafx.h" 或專案設定不一致。 |
| C1083 | Cannot open include file: 'xxx.h': No such file or directory | 找不到包含檔。檢查專案「Additional Include Directories」或檔案是否存在。 |
| C2011 | 'xxx' : 'class' type redefinition | 類型重複定義。常因標頭檔未加防重複巨集(#pragma once 或 #ifndef)且被多次包含。 |
| C2018 | unknown character '0xa3' | 無法識別的字元(全形中文標點或漢字混入程式碼)。改用半形字元或移除中文註解。 |
| C2057 | expected constant expression | 需要常數運算式。switch 的 case 標籤必須為編譯期常數。 |
| C2065 | 'IDD_MYDIALOG' : undeclared identifier | 未宣告的識別碼。資源符號未定義或拼寫錯誤,檢查 resource.h。 |
| C2082 | redefinition of formal parameter 'xxx' | 函式參數在函式主體中被重複定義(如區域變數同名)。 |
| C2143 | syntax error : missing ';' before '{' | 語法錯誤:{ 前缺少分號。常見於類別定義結尾遺漏 ;。 |
| C2146 | syntax error : missing ';' before identifier 'xxx' | 語法錯誤:識別碼前缺少分號。通常是前一行陳述句未結尾。 |
| C2196 | case value 'xxx' already used | switch 中 case 值重複。 |
| C2509 | 'OnTimer' : member function not declared in 'CHelloView' | 成員函數未在類別中宣告。ClassWizard 加入訊息對應時應自動宣告,手動修改易遺漏。 |
| C2511 | 'reset' : overloaded member function not found in 'B' | 重載函數簽章與類別宣告不符。檢查參數型別、const 屬性、呼叫約定。 |
| C2555 | 'B::f1' : overriding virtual function differs from 'A::f1' only by return type | 覆寫虛函數僅回傳型別不同(或呼叫約定不同),C++ 不允許。 |
| C2660 | 'SetTimer' : function does not take 2 parameters | 函數參數個數不符。查閱 MSDN 確認正確參數列表。 |
| C4035 | 'func' : no return value | 非 void 函數缺少 return 陳述句,或所有路徑未回傳值。 |
| C4553 | '==' : operator has no effect; did you intend '='? | == 無副作用,疑為誤寫成 =。常見於 if (a == b) 應為 if (a = b)。 |
| C4700 | local variable 'xxx' used without having been initialized | 區域變數未初始化即使用。宣告時賦初值或確保所有路徑先賦值。 |
| C4716 | 'CMyApp::InitInstance' : must return a value | 非 void 函數必須回傳值。MFC 應用程式類別 InitInstance 需回傳 TRUE/FALSE。 |
| LNK1168 | cannot open Debug/xxx.exe for writing | 無法寫入輸出檔。通常是程式仍在執行(未關閉),或防毒軟體鎖定。 |
| LNK2001 | unresolved external symbol "public: virtual __thiscall Cxxx::~Cxxx(void)" | 連結時找不到外部符號(函數/變數未實作、靜態成員未定義、庫檔未連結)。 |
function call missing argument list:呼叫函數未提供參數列表(如忘記 ())。member function definition looks like a ctor, but name does not match enclosing class:建構函數名稱與類別名稱不符。unexpected end of file while looking for precompiled header directive:編譯非正常終止導致預編譯標頭搜尋失敗。善用「輸出」視窗雙擊錯誤行直接跳轉,配合 MSDN 搜尋錯誤代碼,可大幅縮短除錯時間。