c语⾔常见错误合集
⽂章⽬录
1parison between pointer and integer :
<: 'for' loop initial declarations are only allowed in C99 mode
3.[Error] a function-definition is not allowed here before '{' token
4.[Error] 'f' was not declared in this scope
< C2679
6.Runtime Error
7.[Error] cannot convert 'int (\*)[4]' to 'char (\*)[4]' for argument '1' to 'void row(char (*)[4], int)'
8.[Error] ld returned 1 exit status
9.[Error] expected constructor, destructor, or type conversion before '(' token
10.[Error] 'pair' does not name a type
11.[Error] '>>' should be '> >' within a nested template argument list
12. [Warning] overflow in implicit constant conversion [-Woverflow]
13.[Error] a function-definition is not allowed here before '{' token
14.[Error] reference to 'map' is ambiguous
15.[Error] declaration of 'int x [3]' shadows a parameter
16.[Error] invalid types 'int[int]' for array subscript
[Error] invalid types 'double [100005][double]' for
17.[Error] declaration of 'long long int b' shadows a parameter
18.[Error] expected unqualified-id before 'case'
19.[Error] expected primary-expression before 'case'
<: expected constructor, destructor, or type conversion before '.' token
21.Process exited with return value 3221225477
22.[Error] invalid types 'int[int]' for array subscript
23.[Error] lvalue required as left operand of assignment
24.[Error] base operand of '->' has non-pointer type 'STU {aka student}'
25.[Error] request for member 'score' in '\* o', which is of pointer type 'STU\*
27.提交代码后出现Segmentation Fault
< C2871: 'std' : a namespace with this name does not exist
29.“greater”: 未声明的标识符错误
30.[Error] 'unordered_map' was not declared in this scope
unordered_map头⽂件报错
31.[Error] void value not ignored as it ought to be
32.[Error] 'reverse' was not declared in this scope
33.[Error] incompatible types in assignment of 'int*' to 'int [100]'
34.[Error] expected identifier before '(' token
35.VS⽣成项⽬时报错:“error LNK 1168:⽆法打开进⾏写⼊
36.[Error] request for member 'next' in something not a structure or union
37.[Warning] assignment from incompatible pointer type [enabled by default]
38.[Error] invalid operands to binary - (have 'int' and 'char *')
39.[Error] expected declaration or statement at end of input
41.[Error] variably modified 'ma' at file scope
42.size of array "f" is too large
43.[Error] stray '\241' in program
44.[Error] invalid operands of types '__gnu_cxx::__enable_if
1parison between pointer and integer :
⽐较了两种不同类型的变量,指针和整型
使⽤gcc编译代码是报出错误:
error: ‘for’ loop initial declarations are only allowed in C99 mode
note: use option -std=c99 or -std=gnu99 to compile your code
这是因为在gcc中直接在for循环中初始化了增量:
//这种语法在gcc中是错误的,必须先定义i变量
for (int i = 0; i < len; i++)
1
2
正确写法:
int i;
for (i = 0; i < len; i++)
//这是因为gcc基于c89标准,换成C99标准就可以在for循环内定义i变量了:
//gcc src.c -std=c99 -o src
1
2
3
4
3.[Error] a function-definition is not allowed here before ‘{’ token
检查函数定义的范围 ,在⼀个函数内部不允许再定义函数
4.[Error] ‘f’ was not declared in this scope
f 没有进⾏声明
#include <iostream>
//#include <string>
int main()
{
std::string str = "test";
std::cout <<str<< std::endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
上述代码报错,error: C2679: ⼆进制“<<”: 没有到接受“std::string”类。
iostream ⾥⾯包含的是⽼的string代码(Microsoft Visual Studio 14.0\VC\include) xstring,他的代码并没有重载<<;操作符,如下图:⽽新的C++ 标准string代码(Microsoft Visual Studio 14.0\VC\include\string) 则重载了<<,如下:
所以必须添加头⽂件,⽤最新的标准库string。
6.Runtime Error
运⾏错误,调⽤了没有分配的内存
⼀般就是数组越界,有可能是数组开⼩了,数组还是开⼤点⽐较好
有⼀次是因为没有在读⼊变量n时就使⽤了变量n
还有⼀次是在栈是空的情况下,使⽤了st.top()
7.[Error] cannot convert ‘int (*)[4]’ to ‘char (*)[4]’ for argument ‘1’ to ‘void row(char (*)[4], int)’我这⼀次
是因为⾃定义了⼀个参数为字符数组的⾃定义函数,但是输⼊了⼀个int类型的数组
1.第⼀次是因为很粗⼼地把int main错写成了int msin()
2.第⼆次是因为写错了⾃定义函数名
10.[Error] ‘pair’ does not name a type
没有写using namespace std;
11.[Error] ‘>>’ should be ‘> >’ within a nested template argument list
需要在两个>之间加⼀个空格,
把queue<pair<int,int>> q;变成queue<pair<int,int> > q;
12. [Warning] overflow in implicit constant conversion [-Woverflow]
13.[Error] a function-definition is not allowed here before ‘{’ token
在⼀个函数内部不许再定义函数
14.[Error] reference to ‘map’ is ambiguous
⾃定义的map变量与库中重名,修改为其他变量名即可
同样,有时候end、left、right作为变量名时也会与库中的发⽣冲突
15.[Error] declaration of ‘int x [3]’ shadows a parameter
16.[Error] invalid types ‘int[int]’ for array subscript
误把⼀个int变量当成了数组名使⽤
[Error] invalid types ‘double [100005][double]’ for
同理,把⼀个double型变量当成了数组名
17.[Error] declaration of ‘long long int b’ shadows a parameter
原因是定义了相同的变量
18.[Error] expected unqualified-id before ‘case’
19.[Error] expected primary-expression before ‘case’
把代码中的case改成Case就不会出现这样的情况,应该是case与标准库⾥的名称起了冲突。
21.Process exited with return value 3221225477
这表⽰编译的时候出现了错误,我这次是因为超出了定义的数组的空间。
22.[Error] invalid types ‘int[int]’ for array subscript
我这次是因为定义了⼀个int n;和⼀个int n[100];名称冲突了
24.[Error] base operand of ‘->’ has non-pointer type ‘STU {aka student}’
声明变量时,下⾯这样是定义了⼀个STU *型的变量p和⼀个STU型的变量a。
STU *p, a;
1
如果要定义两个STU *型的变量,需要写成
STU *p, *a;
1
25.[Error] request for member ‘score’ in ‘* o’, which is of pointer type 'STU*
*p->a这样是不对的,应该写成(*p)->a
⼀般这种情况就是缺⼤括号
27.提交代码后出现Segmentation Fault
段错误就是指访问的内存超过了系统所给这个程序的内存空间,需要检查有没有内存溢出
可能是访问了没有规定的内存,⽐如数组的arr[-1]
< C2871: ‘std’ : a namespace with this name does not exist
今天⽤POJ做题才知道,如果要⽤using namespace std;的话前⾯的头⽂件要有#inlcude<iostream>,否则会出现编译错误
29.“greater”: 未声明的标识符错误
头⽂件加上#include<functional>
30.[Error] ‘unordered_map’ was not declared in this scope
unordered_map头⽂件报错
由于Devcpp⽐较⽼了,会出现这个问题
解决⽅法:
1)如果不想修改代码,可以直接在OJ上进⾏编译,OJ的C++版本⼀般⽐较新,都会⽀持unordered_map
如果需要在本地进⾏编译,有如下的⽅法:
31.[Error] void value not ignored as it ought to be
使⽤的⼀个函数的返回值类型是void,⽽有对它进⾏了赋值处理。
32.[Error] ‘reverse’ was not declared in this scope
我这次是因为没有写algorithm头⽂件,reverse函数在这个头⽂件内。
我这次是因为给⼀个⾃定义函数起名为union,改成Union之后就好了
36.[Error] request for member ‘next’ in something not a structure or union
没有正确的使⽤next类型,注意看⼀下next的类型
37.[Warning] assignment from incompatible pointer type [enabled by default]
不同类型的指针进⾏了赋值
38.[Error] invalid operands to binary - (have ‘int’ and ‘char *’)
int类型和char*类型的变量在同⼀⾏
39.[Error] expected declaration or statement at end of input
我这次是因为某个地⽅少了⼀个括号
还有可能是某⼀个函数或者变量没有在使⽤之前声明。
next跟库⾥的属性或⽅法重名了
上⾯的next是和iostream⾥的内容重名了,删除了头⽂件iostream就好了
42.size of array “f” is too large
之前开了⼀个f [ 1 < < 20 + 5 ] [ 20 ] f[1<<20+5][20]f[1<<20+5][20]的数组,报出了上⾯的错误
改成了f [ 1 < < 20 ] [ 20 ] f[1<<20][20]f[1<<20][20]就没事了
43.[Error] stray ‘\241’ in program
所在⾏出现了⾮法字符
晕死,居然是有⼀个看不见的⾮法字符,将报错⾏周围的空⽩都删去就好了
44.[Error] invalid operands of types ‘__gnu_cxx::__enable_if<true, double>::__type {aka double}’ and ‘int’ to binary
‘operator%’
abs函数在C语⾔中包含在 stdlib.h 头⽂件中,在C++中包含在 cmath 头⽂件中
如果⽤的头⽂件是 cmath 并且像下⾯这么写,就会报错
res = abs(a - b) % 2; //报错
1
解决⽅法:
⽅法⼀:⽤⼀个变量来接收 abs(a-b)
d = abs(a - b);
res = d % 2;
1
2
exited⽅法⼆:改⽤ stdlib.h 头⽂件
#include <stdlib.h>
1
45、[Error] assignment to expression with array type
感谢疾风⼤佬的补充
(1)数组不能直接给数组赋值
(2)指针不能直接给数组赋值
解决⽅法:strcpy函数
默_silence
关注
31
6
143
本⽂有帮助的话,就点个赞吧T^T1 年前回复
1
_久夏青:补充:[Error] assignment to expression with array type (1)数组不能直接给数组赋值 (2)指针不能直接给数组赋值 解决⽅法:strcpy函数7 ⽉前回复
_久夏青回复默_silence:嘿嘿嘿 7 ⽉前回复
默_silence回复:感谢您的补充7 ⽉前回复
考过教资:博主你的学习⽅法教导我让我懂了必须要时刻记得⾃⼰犯得错误防⽌第⼆次摔倒在⼀个地⽅1 年前回复
c/c++中常见的错误_Frank.Ginger的博客_c++常见错误
10-22

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。