《Dev-C++编译器》常见语法错误分析
一、缺省分号
错误提示如下:

In function 'main':
[Error] expected ';' before '}' token
说明:1、错误定位在main函数模块内
2、在第8行程序前面缺省分号,即在"{"的前面少了一个“;”,把“;”补上,错误即可消失。
思考题:如下语句:printf("area=%f\n, area); area变量已定义,还错在什么地方?
错误示例: getch( ) }
二、标识符没有定义
错误提示如下:[Error] 'kh' undeclared (first use in this function)
错误分析:标识符“kh”输入有误或还没有定义
错误示例:int x, y, k; kh = 3;
三、缺省分隔符
错误提示如下:[Error]expected '=', ',', ';', 'asm' or '__attribute__' before 'y'
错误分析:在'y'的前面缺省赋值号'=',或 逗号',',或分号';',或'asm'
错误示例:int x y;
四、函数输入有误
错误提示如下:undefined reference to `print'
错误分析:函数句print输入有误或没定义
错误示例:print("area=%f\n", area);
五、文件或目录找不到
错误提示如下:[Error] cono.h: No such file or directory
错误分析:cono.h文件名可能输入错误或者不存在
错误示例:#include <cono.h>
六、非法标点符号
错误提示如下:[Error] stray '\243' in program
或 unknown character ‘0xa3’
错误分析:在光标所在行存在非半角英文标点符号或字符,不能识别。
错误示例: printf("梯形面积=%f\n",y);
七、可执行文件被拒绝执行
错误提示如下: cannot open output file E:\xsp\264\testSize.exe: Permission denied
错误分析:可执行文件testSize.exe正在使用
解决办法:关闭正在运行的testSize.exe
八、没有包含头文件
错误提示如下:[Warning] incompatible implicit declaration of built-in function 'printf' [enabled by default]
错误分析:中文含义“警告:没有声明的都是常用的C库函数。“,这也是它为什么还能编译成功的原因。这种情况,是因为我们没有写函数对应的头文件的原因。
解决办法:只要将包含 printf 函数的头文件包含进来,就可以了。如下所示:
#include <stdio.h>
错误示例:
|
九、无效预处理指令
错误提示如下: invalid preprocessing directive #incde
错误分析:无效预处理指令 incde
解决办法: incde 这个单词拼写错误,应改为 include 。
错误示例:
#incde <stdio.h>
十、头文件名格式错误
错误提示如下:include expects "FILENAME" or <FILENAME>
错误分析:include后接两种格式,如"FILENAME" or <FILENAME>
解决办法:include "stdio.h" 或 include <stdio.h>
错误示例:
#include (stdio.h)
error C2087: '<Unknown>' : 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 中文对照:(编译错误)两个指针量不能相加 分析:例如“int *pa,*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 C2447: missing function header (old-style formal list?) error C2448: '<Unknown>' : 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 loss of 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 '='? 中文对照:(编译警告)“==”运算符无效;是否为“=”? 分析:例如 “i==j;” 语句,“==”运算无意义 |
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”有可能引起逻辑错误 |

