C语言一些常见的错误

文章目录

  • 1.comparison between pointer and integer :
  • 2.error: '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
  • 5.error 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'
  • 20.error: 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\*
  • 26.expected '}' at end if input
  • 27.提交代码后出现Segmentation Fault
  • 28.error 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:无法打开xxxxxx.exe进行写入
  • 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
  • 40.reference to 'next' is ambiguous
  • 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

1.comparison between pointer and integer :

比较了两种不同类型的变量,指针和整型

2.error: ‘for’ loop initial declarations are only allowed in C99 mode

此部分转载自:https://blog.csdn.net/imyang2007/article/details/8296331

使用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++)

正确写法:

int i;
for (i = 0; i < len; i++)
//这是因为gcc基于c89标准,换成C99标准就可以在for循环内定义i变量了:
//gcc src.c -std=c99 -o src

3.[Error] a function-definition is not allowed here before ‘{’ token

检查函数定义的范围 ,在一个函数内部不允许再定义函数

4.[Error] ‘f’ was not declared in this scope

f 没有进行声明

5.error C2679

此部分转载自:https://blog.csdn.net/a379039233/article/details/83755740

#include 
//#include 

int main()
{
    std::string str = "test";
    std::cout <<str<< std::endl;
    return 0;
}

上述代码报错,error: C2679: 二进制“<<”: 没有找到接受“std::string”类。
iostream 里面包含的是老的string代码(Microsoft Visual Studio 14.0\VC\include) xstring,他的代码并没有重载<<操作符,如下图:
C语言一些常见的错误_第1张图片
而新的C++ 标准string代码(Microsoft Visual Studio 14.0\VC\include\string) 则重载了<<,如下:
C语言一些常见的错误_第2张图片
所以必须添加头文件,用最新的标准库string。

6.Runtime Error

运行错误,调用了没有分配的内存

  1. 一般就是数组越界,有可能是数组开小了,数组还是开大点比较好
  2. 有一次是因为没有在读入变量n时就使用了变量n
  3. 还有一次是在栈是空的情况下,使用了st.top()

7.[Error] cannot convert ‘int (*)[4]’ to ‘char (*)[4]’ for argument ‘1’ to ‘void row(char (*)[4], int)’

我这一次是因为自定义了一个参数为字符数组的自定义函数,但是输入了一个int类型的数组

8.[Error] ld returned 1 exit status

转载自:https://www.cnblogs.com/kinson/p/7922225.html
C语言一些常见的错误_第3张图片
1.第一次是因为很粗心地把int main错写成了int msin()

2.第二次是因为写错了自定义函数名

9.[Error] expected constructor, destructor, or type conversion before ‘(’ token

此部分转载自:https://www.cnblogs.com/xiaoZQ/p/5203580.html
C语言一些常见的错误_第4张图片

10.[Error] ‘pair’ does not name a type

没有写using namespace std;

11.[Error] ‘>>’ should be ‘> >’ within a nested template argument list

需要在两个>之间加一个空格,

把queue> q;变成queue > q;

12. [Warning] overflow in implicit constant conversion [-Woverflow]

C语言一些常见的错误_第5张图片

13.[Error] a function-definition is not allowed here before ‘{’ token

在一个函数内部不许再定义函数

14.[Error] reference to ‘map’ is ambiguous

自定义的map变量与库中重名,修改为其他变量名即可

同样,有时候end作为变量名时也会与库中的发生冲突

15.[Error] declaration of ‘int x [3]’ shadows a parameter

C语言一些常见的错误_第6张图片

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与标准库里的名称起了冲突。

20.error: expected constructor, destructor, or type conversion before ‘.’ token

链接:https://blog.csdn.net/glp_hit/article/details/8635049

21.Process exited with return value 3221225477

这表示编译的时候出现了错误,我这次是因为超出了定义的数组的空间。

22.[Error] invalid types ‘int[int]’ for array subscript

我这次是因为定义了一个int n;和一个int n[100];名称冲突了

23.[Error] lvalue required as left operand of assignment

https://blog.csdn.net/kerouacs/article/details/79291762

24.[Error] base operand of ‘->’ has non-pointer type ‘STU {aka student}’

声明变量时,下面这样是定义了一个STU *型的变量p和一个STU型的变量a。

STU *p, a;

如果要定义两个STU *型的变量,需要写成

STU *p, *a;

25.[Error] request for member ‘score’ in ‘* o’, which is of pointer type 'STU*

*p->a这样是不对的,应该写成(*p)->a

26.expected ‘}’ at end if input

在这里插入图片描述
一般这种情况就是缺大括号

27.提交代码后出现Segmentation Fault

段错误就是指访问的内存超过了系统所给这个程序的内存空间,需要检查有没有内存溢出

可能是访问了没有规定的内存,比如数组的arr[-1]

https://blog.csdn.net/u010150046/article/details/77775114

28.error C2871: ‘std’ : a namespace with this name does not exist

今天用POJ做题才知道,如果要用using namespace std;的话前面的头文件要有#inlcude,否则会出现编译错误

链接:https://blog.csdn.net/zhenshiyiqie/article/details/8445237

29.“greater”: 未声明的标识符错误

头文件加上#include

30.[Error] ‘unordered_map’ was not declared in this scope

unordered_map头文件报错

由于Devcpp比较老了,会出现这个问题

解决方法:

1)如果不想修改代码,可以直接在OJ上进行编译,OJ的C++版本一般比较新,都会支持unordered_map

如果需要在本地进行编译,有如下的方法:

2)https://blog.csdn.net/fantasy_94/article/details/86425018

3)https://www.cnblogs.com/llllrj/p/9510239.html

31.[Error] void value not ignored as it ought to be

使用的一个函数的返回值类型是void,而有对它进行了赋值处理。

32.[Error] ‘reverse’ was not declared in this scope

我这次是因为没有写algorithm头文件,reverse函数在这个头文件内。

33.[Error] incompatible types in assignment of ‘int*’ to ‘int [100]’

https://zhidao.baidu.com/question/137297696.html

34.[Error] expected identifier before ‘(’ token

https://blog.csdn.net/qq_40732350/article/details/82946117

我这次是因为给一个自定义函数起名为union,改成Union之后就好了

35.VS生成项目时报错:“error LNK 1168:无法打开xxxxxx.exe进行写入

https://blog.csdn.net/qq_27565063/article/details/80658718

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

我这次是因为某个地方少了一个括号

还有可能是某一个函数或者变量没有在使用之前声明。

40.reference to ‘next’ is ambiguous

next跟库里的属性或方法重名了

上面的next是和iostream里的内容重名了,删除了头文件iostream就好了

41.[Error] variably modified ‘ma’ at file scope

https://blog.csdn.net/zhaohaibo_/article/details/89322902

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

所在行出现了非法字符

晕死,居然是有一个看不见的非法字符,将报错行周围的空白都删去就好了

https://blog.csdn.net/pijiuxiaolongxia/article/details/90145731

44.[Error] invalid operands of types ‘__gnu_cxx::__enable_if::__type {aka double}’ and ‘int’ to binary ‘operator%’

abs函数在C语言中包含在 stdlib.h 头文件中,在C++中包含在 cmath 头文件中

如果用的头文件是 cmath 并且像下面这么写,就会报错

res = abs(a - b) % 2; //报错

解决方法:

方法一:用一个变量来接收 abs(a-b)

d = abs(a - b);
res = d % 2;

方法二:改用 stdlib.h 头文件

#include 

你可能感兴趣的:(程序设计基础:C语言)