C語言開發中常見報錯的解決方案
整理來源於網絡,侵權請通知刪除。*禁止轉載
fatal error C1003: error count exceeds number; stopping compilation
中文對照:(編譯錯誤)錯誤太多,停止編譯
解決方案:修改之前的錯誤,再次編譯
fatal error C1004: unexpected end of file found
中文對照:(編譯錯誤)文件未結束
解決方案:一個函數或者一個結構定義缺少“}”、或者在一個函數調用或表達式中括號沒有配對出現、或者注釋符“/…/”不完整等
fatal error C1083: Cannot open include file: 'xxx': No such file or directory
中文對照:(編譯錯誤)無法打開頭文件 xxx:沒有這個文件或路徑
解決方案:頭文件不存在、或者頭文件拼寫錯誤、或者文件為只讀
fatal error C1903: unable to recover from previous error(s); stopping compilation
中文對照:(編譯錯誤)無法從之前的錯誤中恢復,停止編譯
解決方案:引起錯誤的原因很多,建議先修改之前的錯誤
error C2001: newline in constant
中文對照:(編譯錯誤)常量中創建新行
解決方案:字符串常量多行書寫
error C2006: #include expected a filename, found 'identifier'
中文對照:(編譯錯誤)#include 命令中需要文件名
解決方案:一般是頭文件未用一對雙引號或尖括號括起來,例如“#include stdio.h”
error C2007: #define syntax
中文對照:(編譯錯誤)#define 語法錯誤
解決方案:例如“#define”后缺少宏名,例如“#define”
error C2008: 'xxx' : unexpected in macro definition
中文對照:(編譯錯誤)宏定義時出現了意外的 xxx
解決方案:宏定義時宏名與替換串之間應有空格,例如“#define TRUE"1"”
error C2009: reuse of macro formal 'identifier'
中文對照:(編譯錯誤)帶參宏的形式參數重復使用
解決方案:宏定義如有參數不能重名,例如“#define s(a,a) (a*a)”中參數 a 重復
error C2010: 'character' : unexpected in macro formal parameter list
中文對照:(編譯錯誤)帶參宏的形式參數表中出現未知字符
解決方案:例如“#define s(r|) r*r”中參數多了一個字符‘|’
error C2014: preprocessor command must start as first nonwhite space
中文對照:(編譯錯誤)預處理命令前面只允許空格
解決方案:每一條預處理命令都應獨占一行,不應出現其他非空格字符
error C2015: too many characters in constant
中文對照:(編譯錯誤)常量中包含多個字符
解決方案:字符型常量的單引號中只能有一個字符,或是以“\”開始的一個轉義字符, 例如“char error = 'error';”
error C2017: illegal escape sequence
中文對照:(編譯錯誤)轉義字符非法
解決方案:一般是轉義字符位於 ' ' 或 " " 之外,例如“char error = ' '\n;”
error C2018: unknown character '0xhh'
中文對照:(編譯錯誤)未知的字符 0xhh
解決方案:一般是輸入了中文標點符號,例如“char error = 'E';”中“;”為中文標點符號
error C2019: expected preprocessor directive, found 'character'
中文對照:(編譯錯誤)期待預處理命令,但有無效字符
解決方案:一般是預處理命令的#號后誤輸入其他無效字符,例如“#!define TRUE 1”
error C2021: expected exponent value, not 'character'
中文對照:(編譯錯誤)期待指數值,不能是字符
解決方案:一般是浮點數的指數表示形式有誤,例如 123.456E
error C2039: 'identifier1' : is not a member of 'identifier2'
中文對照:(編譯錯誤)標識符 1 不是標識符 2 的成員
解決方案:程序錯誤地調用或引用結構體、共用體、類的成員
error C2041: illegal digit 'x' for base 'n'
中文對照:(編譯錯誤)對於 n 進制來說數字 x 非法
解決方案:一般是八進制或十六進制數表示錯誤,例如“int i = 081;”語句中數字‘8’不是八進制的基數
rror C2048: more than one default
中文對照:(編譯錯誤)default 語句多於一個
解決方案:switch 語句中只能有一個 default,刪去多余的 default
error C2050: switch expression not integral
中文對照:(編譯錯誤)switch 表達式不是整型的
解決方案:switch 表達式必須是整型(或字符型),例如“switch ("a")”中表達式為字符串,這是非法的
error C2051: case expression not constant
中文對照:(編譯錯誤)case 表達式不是常量
解決方案:case 表達式應為常量表達式,例如“case"a"”中“"a"”為字符串,這是非法的
error C2052: 'type' : illegal type for case expression
中文對照:(編譯錯誤)case 表達式類型非法
解決方案:case 表達式必須是一個整型常量(包括字符型)
error C2057: expected constant expression
中文對照:(編譯錯誤)期待常量表達式
解決方案:一般是定義數組時數組長度為變量,例如“int n=10; int a[n];”中 n 為變量,這是非法的
error C2058: constant expression is not integral
中文對照:(編譯錯誤)常量表達式不是整數
解決方案:一般是定義數組時數組長度不是整型常量
error C2059: syntax error : 'xxx'
中文對照:(編譯錯誤)‘xxx’語法錯誤
解決方案:引起錯誤的原因很多,可能多加或少加了符號 xxx
error C2064: term does not evaluate to a function
中文對照:(編譯錯誤)無法識別函數語言
解決方案:
(1)函數參數有誤,表達式可能不正確,例如“sqrt(s(s-a)(s-b)(s-c));”中表達式不正確
(2)變量與函數重名或該標識符不是函數,例如“int i,j; j=i();”中 i 不是函數
error C2065: 'xxx' : undeclared identifier
中文對照:(編譯錯誤)未定義的標識符 xxx
解決方案:
(1) 如果 xxx 為 cout、cin、scanf、printf、sqrt 等,則程序中包含頭文件有誤
(2) 未定義變量、數組、函數原型等,注意拼寫錯誤或區分大小寫。
error C2078: too many initializers
中文對照:(編譯錯誤)初始值過多
解決方案:一般是數組初始化時初始值的個數大於數組長度,例如“int b[2]={1,2,3};”
error C2082: redefinition of formal parameter 'xxx'
中文對照:(編譯錯誤)重復定義形式參數 xxx
解決方案:函數首部中的形式參數不能在函數體中再次被定義
error C2084: function 'xxx' already has a body
中文對照:(編譯錯誤)已定義函數 xxx
解決方案:在 VC++早期版本中函數不能重名,6.0版本中支持函數的重載,函數名可以相同但參數不一樣
error C2086: 'xxx' : redefinition
中文對照:(編譯錯誤)標識符 xxx 重定義
解決方案:變量名、數組名重名
error C2087: '
' : missing subscript
中文對照:(編譯錯誤)下標未知
解決方案:一般是定義二維數組時未指定第二維的長度,例如“int a[3][];”
error C2100: illegal indirection
中文對照:(編譯錯誤)非法的間接訪問運算符“”
解決方案:對非指針變量使用“”運算
error C2105: 'operator' needs l-value
中文對照:(編譯錯誤)操作符需要左值
解決方案:例如“(a+b)++;”語句,“++”運算符無效
error C2106: 'operator': left operand must be l-value
中文對照:(編譯錯誤)操作符的左操作數必須是左值
解決方案:例如“a+b=1;”語句,“=”運算符左值必須為變量,不能是表達式
error C2110: cannot add two pointers
中文對照:(編譯錯誤)兩個指針量不能相加
解決方案:例如“intpa,pb,*a;a=pa+pb;”中兩個指針變量不能進行“+”運算
error C2117: 'xxx' : array bounds overflow
中文對照:(編譯錯誤)數組 xxx 邊界溢出
解決方案:一般是字符數組初始化時字符串長度大於字符數組長度,例如“char str[4] = "abcd";”
error C2118: negative subscript or subscript is too large
中文對照:(編譯錯誤)下標為負或下標太大
解決方案:一般是定義數組或引用數組元素時下標不正確
error C2124: divide or mod by zero
中文對照:(編譯錯誤)被零除或對 0 求余
解決方案:例如“int i = 1 / 0;”除數為 0
error C2133: 'xxx' : unknown size
中文對照:(編譯錯誤)數組 xxx 長度未知
解決方案:一般是定義數組時未初始化也未指定數組長度,例如“int a[];”
error C2137: empty character constant。
中文對照:(編譯錯誤)字符型常量為空
解決方案:一對單引號“''”中不能沒有任何字符
error C2143: syntax error : missing 'token1' before 'token2'
error C2146: syntax error : missing 'token1' before identifier 'identifier'
中文對照:(編譯錯誤)在標識符或語言符號 2 前漏寫語言符號 1
解決方案:可能缺少“{”、“)”或“;”等語言符號
error C2144: syntax error : missing ')' before type 'xxx'
中文對照:(編譯錯誤)在 xxx 類型前缺少‘)’
解決方案:一般是函數調用時定義了實參的類型
error C2181: illegal else without matching if
中文對照:(編譯錯誤)非法的沒有與 if 相匹配的 else
解決方案:可能多加了“;”或復合語句沒有使用“{}”
error C2196: case value '0' already used
中文對照:(編譯錯誤)case 值 0 已使用
解決方案:case 后常量表達式的值不能重復出現
error C2296: '%' : illegal, left operand has type 'float'
error C2297: '%' : illegal, right operand has type 'float'
中文對照:(編譯錯誤)%運算的左(右)操作數類型float,這是非法的
解決方案:求余運算的對象必須均為int類型,應正確定義變量類型或使用強制類型轉換
error C2371: 'xxx' : redefinition; different basic types
中文對照:(編譯錯誤)標識符 xxx 重定義;基類型不同
解決方案:定義變量、數組等時重名
error C2440: '=' : cannot convert from 'char [2]' to 'char'
中文對照:(編譯錯誤)賦值運算,無法從字符數組轉換為字符
解決方案:不能用字符串或字符數組對字符型數據賦值,更一般的情況,類型無法轉換
error C2448: '
' : function-style initializer appears to be a function definition
中文對照:(編譯錯誤)缺少函數標題(是否是老式的形式表?)
解決方案:函數定義不正確,函數首部的“()”后多了分號或者采用了老式的 C 語言的形參表
error C2450: switch expression of type 'xxx' is illegal
中文對照:(編譯錯誤)switch 表達式為非法的 xxx 類型
解決方案:switch 表達式類型應為 int 或 char
error C2466: cannot allocate an array of constant size 0
中文對照:(編譯錯誤)不能分配長度為 0 的數組
解決方案:一般是定義數組時數組長度為 0
error C2601: 'xxx' : local function definitions are illegal
中文對照:(編譯錯誤)函數 xxx 定義非法
解決方案:一般是在一個函數的函數體中定義另一個函數
error C2632: 'type1' followed by 'type2' is illegal
中文對照:(編譯錯誤)類型 1 后緊接着類型 2,這是非法的
解決方案:例如“int float i;”語句
error C2660: 'xxx' : function does not take n parameters
中文對照:(編譯錯誤)函數 xxx 不能帶 n 個參數
解決方案:調用函數時實參個數不對,例如“sin(x,y);”
error C2664: 'xxx' : cannot convert parameter n from 'type1' to 'type2'
中文對照:(編譯錯誤)函數 xxx 不能將第 n 個參數從類型 1 轉換為類型 2
解決方案:一般是函數調用時實參與形參類型不一致
error C2676: binary '<<' : 'class istream_withassign' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2676: binary '>>' : 'class ostream_withassign' does not define this operator or a conversion to a type acceptable to the predefined operator
解決方案:“>>”、“<<”運算符使用錯誤,例如“cin<<x; cout>>y;”
error C4716: 'xxx' : must return a value
中文對照:(編譯錯誤)函數 xxx 必須返回一個值
解決方案:僅當函數類型為 void時,才能使用沒有返回值的返回命令。
fatal error LNK1104: cannot open file "Debug/Cpp1.exe"
中文對照:(鏈接錯誤)無法打開文件 Debug/Cpp1.exe
解決方案:重新編譯鏈接
fatal error LNK1168: cannot open Debug/Cpp1.exe for writing
中文對照:(鏈接錯誤)不能打開 Debug/Cpp1.exe文件,以改寫內容。
解決方案:一般是 Cpp1.exe 還在運行,未關閉
fatal error LNK1169: one or more multiply defined symbols found
中文對照:(鏈接錯誤)出現一個或更多的多重定義符號。
解決方案:一般與 error LNK2005 一同出現
error LNK2001: unresolved external symbol _main
中文對照:(鏈接錯誤)未處理的外部標識 main
解決方案:一般是 main 拼寫錯誤,例如“void mian()”
error LNK2005: _main already defined in Cpp1.obj
中文對照:(鏈接錯誤)main 函數已經在 Cpp1.obj 文件中定義
解決方案:未關閉上一程序的工作空間,導致出現多個 main 函數
warning C4003: not enough actual parameters for macro 'xxx'
中文對照:(編譯警告)宏 xxx 沒有足夠的實參
解決方案:一般是帶參宏展開時未傳入參數
warning C4067: unexpected tokens following preprocessor directive -expected a newline
中文對照:(編譯警告)預處理命令后出現意外的符號-期待新行
解決方案:“#include<iostream.h>;”命令后的“;”為多余的字符
warning C4091: '' : ignored on left of 'type' when no variable is declared
中文對照:(編譯警告)當沒有聲明變量時忽略類型說明
解決方案:語句“int ;”未定義任何變量,不影響程序執行
warning C4101: 'xxx' : unreferenced local variable
中文對照:(編譯警告)變量xxx定義了但未使用
解決方案:可去掉該變量的定義,不影響程序執行
warning C4244: '=' : conversion from 'type1' to 'type2', possible lossof data
中文對照:(編譯警告)賦值運算,從數據類型 1 轉換為數據類型 2,可能丟失數據
解決方案:需正確定義變量類型,數據類型1為float或double、數據類型 2 為 int時,結果有可能不正確,數據類型 1 為 double、數據類型 2 為 float 時,不影響程序結果,可忽略該警告
warning C4305: 'initializing' : truncation from 'const double' to 'float'
中文對照:(編譯警告)初始化,截取雙精度常量為 float 類型
解決方案:出現在對 float 類型變量賦值時,一般不影響最終結果
warning C4390: ';' : empty controlled statement found; is this the intent?
中文對照:(編譯警告)‘;’控制語句為空語句,是程序的意圖嗎?
解決方案:if 語句的分支或循環控制語句的循環體為空語句,一般是多加了“;”
warning C4508: 'xxx' : function should return a value; 'void' return type assumed
中文對照:(編譯警告)函數 xxx 應有返回值,假定返回類型為 void
解決方案:一般是未定義 main 函數的類型為 void,不影響程序執行
warning C4552: 'operator' : operator has no effect; expected operator with side-effect
中文對照:(編譯警告)運算符無效果;期待副作用的操作符
解決方案:例如“i+j;”語句,“+”運算無意義
warning C4553: '' : operator has no effect; did you intend '='?
中文對照:(編譯警告)“”運算符無效;是否為“=”?
解決方案:例如 “ij;” 語句,“”運算無意義
warning C4700: local variable 'xxx' used without having been initialized
中文對照:(編譯警告)變量 xxx 在使用前未初始化
解決方案:變量未賦值,結果有可能不正確,如果變量通過 scanf 函數賦值,則有可能漏寫“&”運算符,或變量通過 cin 賦值,語句有誤
warning C4715: 'xxx' : not all control paths return a value
中文對照:(編譯警告)函數 xxx 不是所有的控制路徑都有返回值
解決方案:一般是在函數的 if 語句中包含 return 語句,當 if 語句的條件不成立時沒有返回值
warning C4723: potential divide by 0
中文對照:(編譯警告)有可能被 0 除
解決方案:表達式值為 0 時不能作為除數
warning C4804: '<' : unsafe use of type 'bool' in operation
中文對照:(編譯警告)‘<’:不安全的布爾類型的使用
解決方案:例如關系表達式“0<=x<10”有可能引起邏輯錯誤