C语言常见编译错误分类及其解决方案

C语言常见编译错误分类及其解决方案

文章目录绪论第一安装错误:[1]Error spawning cl.exe[2]fatal error C1083: Cannot open include file:第二打开编译器失败,提示不能打开第三cannot execute program解决方案第四连接Link.exe错误解决方案第五 error C2143: syntax error : missing ‘)’ before ‘return’解决方案第六涉及`scanf`输出结果为空的错误解决方案第七error C2065: ‘a’ : undeclared identifiererror C2109: subscript requires array or pointer typeerror C2102: ‘&’ requires l-value第八error C2018: unknown character ‘0xa3’error C2018: unknown character ‘0xac’第九fatal error C1083: Cannot open include file: ‘stduio.h’: No such file or directory文字说明fatal error C1003: error count exceeds number; stopping compilationfatal error C1004: unexpected end of file foundfatal error C1083: Cannot open include file: ‘xxx’: No such file or directoryfatal error C1903: unable to recover from previous error(s); stopping compilationerror C2001: newline in constanterror C2006: #include expected a filename, found ‘identifier’error C2007: #define syntaxerror C2008: ‘xxx’ : unexpected in macro definitionerror C2009: reuse of macro formal ‘identifier’error C2010: ‘character’ : unexpected in macro formal parameter listerror C2014: preprocessor command must start as first nonwhite spaceerror C2015: too many characters in constanterror C2017: illegal escape sequenceerror C2018: unknown character ‘0xhh’error C2019: expected preprocessor directive, found ‘character’*error C2021: expected exponent value, not ‘character’*error C2039: ‘identifier1’ : is not a member of ‘identifier2’error C2041: illegal digit ‘x’ for base ‘n’error C2048: more than one defaulterror C2050: switch expression not integralerror C2051: case expression not constanterror C2052: ‘type’ : illegal type for case expressionerror C2057: expected constant expressionerror C2058: constant expression is not integralerror C2059: syntax error : ‘xxx’error C2064: term does not evaluate to a functionerror C2065: ‘xxx’ : undeclared identifiererror C2078: too many initializerserror C2082: redefinition of formal parameter ‘xxx’error C2084: function ‘xxx’ already has a bodyerror C2086: ‘xxx’ : redefinitionerror C2087: ‘’ : missing subscripterror C2100: illegal indirectionerror C2105: ‘operator’ needs l-valueerror C2296: ‘%’ : illegal, left operand has type ‘float’ error C2297: ‘%’ : illegal, right operand has type ‘float’error C2371: ‘xxx’ : redefinition; different basic typeserror C2440: ‘=’ : cannot convert from ‘char [2]’ to ‘char’error C2447: missing function header (old-style formal list?) error C2448: ‘’ : function-style initializer appears to be a function definitionerror C2450: switch expression of type ‘xxx’ is illegalerror C2466: cannot allocate an array of constant size 0error C2601: ‘xxx’ : local function definitions are illegalerror C2632: ‘type1’ followed by ‘type2’ is illegalerror C2660: ‘xxx’ : function does not take n parameterserror C2664: ‘xxx’ : cannot convert parameter n from ‘type1’ to ‘type2’error C2676: binary ‘’ : ‘class ostream_withassign’ does not define this operator or a conversion to a type acceptable to the predefined operator

绪论

     经过两三次实验,相信大家上机都发现自己出bug的情况了吧,今天学长就把bug汇总一下,这篇文章会慢慢完善,学长根据自己的经验进行汇总直到把大部分bug收集起来,这些都是学长自己做的,没有照搬网上的,因为很多网上没有或是并不好理解,希望老师同学支持哈。

首先我们来分类一下几种常见错误:

逻辑错误语法错误链接错误编译错误安装错误第一安装错误:[1]Error spawning cl.exe[2]fatal error C1083: Cannot open include file:

以上两种错误情况是一样的,头文件没有放到安装目录下,都是你在安装的时候路径选择错了,怎么解决呢?重新安装??这不能解决问题的!!!解决方案:我们首先点击可执行文件,然后在路径区将前四个路径分别做如下修改,和我下边的地址设置成一样!!如果你的前四个文件路径和我下边的不一样,那你就双击一下,修改地址即可。双击之后点击那三个点就可以选择文件路径了!!接下来目录选择头文件include区域!和上一步操作一样,把路径地址换成我下面的路径,记住如果这两你多余四行,那你就删除,删到只剩三行即可。接下来选择目录如下,同时修改前两行地址,和下边的地址要一样,多余行数删除处理!最后一步,选择另外一个目录,换掉前三行地址,和如下地址要一样!!做完如上操作之后点击确定在进行编译,问题就解决了,如果没有解决那就是你的某个目录下的文件没有按照我的地址来配对,再检查一下就行了!

第二打开编译器失败,提示不能打开

这个问题的其实很简单的。解决方案:

点击进入—->下载该解压器安装好之后,观察压缩包变成360zip之后就可以直接打开了!第三cannot execute program解决方案

问题导致原因:你添加的工程名字和文件名字不一致解决方案:首先建好项目名字,在建你的C++source时,记得名字一定要一样!!!还有不能用中文名字!!

第四连接Link.exe错误解决方案

错误原因:一个项目建了多个工程文件,且名字不对应,与第三错误类似

解决方案:重新新建一个项目,再建一个工程文件,记得名字要一样!!

第五 error C2143: syntax error : missing ‘)’ before ‘return’解决方案

当发现这个语句的时候发现只要我们对应去找提示错误的语句就可以了,把失去的分号去掉就可以了!!!

可是我们发现,提示的语句是有分号的!!!这个时候我们要去该语句的上一句或是下一句去寻找分号是否有缺失,双引号也是我们排查的目标!!!我们发现其实上一个语句缺了分号这个错误是很多人都容易忽略的,也是不容易看出来的,但一定要有这个思路!!

第六涉及scanf输出结果为空的错误解决方案

我们在输入一个值后输出缺为空!!排查错误步骤:

输入语句格式【忘了地址符&】输出语句格式【忘了逗号】变量定义数据类型【数据类型不匹配】加上地址符&之后就OK了第七error C2065: ‘a’ : undeclared identifiererror C2109: subscript requires array or pointer typeerror C2102: ‘&’ requires l-value

分析:凡是发现连带错误的时候,都要自顶向下的检查拍错!看代码的时候也是自顶向下进行排查!错误解决方案:把for循环后边的分号去掉!!你只要记住,也就是循环,选择结构的都不要再后边加分号。for和if不要再后边加分号。

第八error C2018: unknown character ‘0xa3’error C2018: unknown character ‘0xac’

我相信这个错误大家已经见得司空见惯了吧!!!是不是一脸懵逼???解决方案:检查标点符号!检查标点符号!检查标点符号!

注意你的标点符号,有一个地方的标点用成了中文!!!发现句子外边的分号正常那就看中间句子的空格间隙是不是特别大,如果大,那就说明符号有问题!!

第九fatal error C1083: Cannot open include file: ‘stduio.h’: No such file or directory

这个是不是特别眼熟??解决方案:观察头文件,你的头文件拼错了!!!你们顺手就喜欢打一个u在上面,正确头文件:

文字说明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’不是八进制的基数

error 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 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: ‘’ : 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 ostream_withassign’ does not define this operator or a conversion to a type acceptable to the predefined operator

分析:“>>”、“


比丘资源网 » C语言常见编译错误分类及其解决方案

发表回复

提供最优质的资源集合

立即查看 了解详情