第一要則,不要直接點擊”編譯並且運行”,而是應該點擊”編譯(build)”按鈕,這樣可以保證警告不會被忽略,一些警告是非常有用的.
第二要則:有多個錯誤,要先處理最前面的錯誤,因為后面的錯誤可能前面的錯誤引發的.所以修改最前面的錯誤后就可以立即重新編譯,往往可以看到所有的錯誤信息都不見了.
每一條信息是按字母排序,可以按序查找。
格式說明: 每一條錯誤都提供了樣例程序,編譯信息以及必要的說明
編譯信息格式按 文件名,程序行數,編譯錯誤信息組織,例如
E:\chen\dream\ex.c|6|error: break statement not within loop or switch|
使用說明: 使用關鍵字查找本文,如undeclared
a label can only be part of a statement and a declaration is not a statement|
break statement not within loop or switch
character constant too long for its type
'else' without a previous 'if'
expected declaration or statement at end of input
expected declaration specifiers or '...' before string constant
expected expression before 'X' token
expected primary-expression before '=' token
expected identifier or '(' before '}' token
expected '=', ',', ';', 'asm' or '__attribute__' before 'X' token
extra tokens at end of #include directive [enabled by default]|
format '%X' expects argument of type 'XXX',
implicit declaration of function 'XXX'
incompatible type for argument X of 'XXX
invalid preprocessing directive 'XXX
invalid suffix "X" on integer constant
lvalue required as left operand of assignment
missing terminating X character
情況一: missing terminating " character 漏掉了右側的雙引號
fatal error: XXX: No such file or directory
subscripted value is neither array nor pointer nor vector
suggest parentheses around '&&' within '||'(警告)
suggest parentheses around assignment used as truth value
too few arguments to function 'XXX'
a label can only be part of a statement and a declaration is not a statement|
標號不應該放在聲明處
#include<stdio.h>
int main(void)
{
backward: //應該放在int語句下面
int i = 0, n, a[32];
scanf("%d", &n);
while(n > 0) {
a[i] = n % 2;
i = i + 1;
n = n / 2;
}
for(i--; i >= 0; i--)
printf("%d", a[i]);
printf("\n");
goto backward;
return 0;
}
E:\chen\dream\ex.c|5|error: a label can only be part of a statement and a declaration is not a statement|
break statement not within loop or switch
break必須在循環或switch里面。如果在if里面,if必須在循環或switch里面。
示例
#include <stdio.h>
int main(void)
{
int i=1;
if(i==1)
break;//在if里面是不可以的
return 0;
}
E:\chen\dream\ex.c|6|error: break statement not within loop or switch|
character constant too long for its type
#include <stdio.h>
int main()
{
char *s = 'hello,world\n'; //應該用雙引號
return 0;
}
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|4|warning: character constant too long for its type [enabled by default]|
E:\chen\dream\ex.c|4|warning: initialization makes pointer from integer without a cast [enabled by default]|
解釋:字符常量才用單引號
conflicting types for ‘XXX’
#include<stdio.h>
#include<math.h>
double pow(int x,int y) //math.h中已經有pow函數,兩者不一致,可以取另外的名字
{
return x*y;
}
int main(void)
{
pow(3,5);
return 0;
}
E:\chen\dream\ex.c|3|error: conflicting types for 'pow'|
'else' without a previous 'if'
解釋:else沒有配對的if。
#include <stdio.h>
int main(void)
{
int year = 3;
scanf("%d", &year);
if(year % 7); //此處分號多余,導致后面的else沒有配對
printf("yes\n");
else
printf("no\n");
return 0;
}
E:\chen\dream\ex.c|8|error: 'else' without a previous 'if'
empty character constant
#include <stdio.h>
int main(void)
{
char s[10]="123";
if(s[0] == '') //單引號中間有空格,而不是空的沒有字符
printf("equal\n");
return 0;
}
E:\chen\dream\ex.c|5|error: empty character constant|
expected declaration or statement at end of input
#include <stdio.h>
int main()
{
int a = 3;
return 0; //后面漏掉}
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|error: expected declaration or statement at end of input|
解釋: 程序沒有寫完整,漏掉了大括號。
expected declaration specifiers or '...' before string constant
int a,b;
scanf("%d %d",&a, &b); //連個main函數都沒有
E:\chen\dream\ex.c|2|error: expected declaration specifiers or '...' before string constant|
expected expression before 'X' token
一般是因為寫錯了符號或者漏寫了符號或者多敲了符號
示例一
#include <stdio.h>
int main(void)
{
int a;
scanf(%d", &a); //%前面少了雙引號
return 0;
}
E:\chen\dream\ex.c|5|error: expected expression before '%' token|
示例二
#include <stdio.h>
int main(void)
{
int a = 3;
if(a= = 3) //==之間不能有空格
print("yes");
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|6|error: expected expression before '=' token|
E:\chen\dream\ex.c|7|warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]|
E:\chen\dream\ex.c|5|warning: variable 'a' set but not used [-Wunused-but-set-variable]|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
示例三
if(s[i]=='a'||s[i]=='A')||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')
error: expected expression before '||' token
注意'A'的后面多寫了)
expected primary-expression before '=' token
if(l!==l1) //多寫了=
error: expected primary-expression before '=' token
expected identifier or '(' before '}' token
示例一
#include <stdio.h>
int main()
{
return 0;
}
} //此處括號是多余的
E:\chen\dream\ex.c|7|error: expected identifier or '(' before '}' token|
示例二
#include <stdio.h>
int main();
{ //其實是這一行的前面多了分號
return 0;
}
E:\chen\dream\ex.c|3|error: expected identifier or '(' before '{' token|
expected '=', ',', ';', 'asm' or '__attribute__' before 'X' token
少了或者多寫了符號
示例一
#include <stdio.h>
int main
{ //實際是main函數后面少了( )
return 0;
}
E:\chen\dream\ex.c|3|error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token|
示例二
include <stdio.h> //include前面少了#
int main()
{
return 0;
}
E:\chen\dream\ex.c|1|error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token|
示例三
#include <stdio.h>
int main(void)
{
int a
scanf("%d",&a); //前一行漏掉一個分號
return 0;
}
E:\chen\dream\ex.c|5|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'scanf'|
示例四
int flag==0; //多寫了一個=
error: expected '=', ',', ';', 'asm' or '__attribute__' before '==' token
expected ';' before ‘XXX’
少了分號
示例一
#include <stdio.h>
int main()
{
printf("hello,world\n")
return 0; //前一行漏掉一個分號
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|error: expected ';' before 'return'|
E:\chen\dream\ex.c|6|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
原因: 上一行的語句少了分號結束(本例中是printf這一行少了分號,但是編譯器顯示錯誤在return 0這一行).
示例二
#include <stdio.h>
int main(void)
{
int i;
for(i=1, i<10; i++) //for語句里面應該恰好有兩個分號,i=1后面應該是分號
printf("%d",i);
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|warning: value computed is not used [-Wunused-value]|
E:\chen\dream\ex.c|5|error: expected ';' before ')' token|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
expected '(' before '{' token
for{b=1;b<=50;b++} //顯然(被誤寫成了{
error: expected '(' before '{' token
expected ')' before 'X' token
一般是漏掉了)號,但是也有其他情況。
示例一
#include <stdio.h>
int main()
{
printf("hello,world\n"; //此處;前面漏掉了)
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|4|error: expected ')' before ';' token|
E:\chen\dream\ex.c|6|error: expected ';' before '}' token|
E:\chen\dream\ex.c|6|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 2 second(s)) ===|
示例二
#include <stdio.h>
int main()
{
int a = 3;
printf("%d%d"a); //a前面有應該逗號把兩個參數分開
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|error: expected ')' before 'a'|
E:\chen\dream\ex.c|5|warning: format '%d' expects a matching 'int' argument [-Wformat]|
E:\chen\dream\ex.c|4|warning: unused variable 'a' [-Wunused-variable]|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
示例三
printf("GCD=%d LCM=%d",gcd(m,n) lcm(m,n)); //lcm前面應該有逗號
Main.c:24:37: error: expected ')' before 'lcm'
示例四
while(scanf("%d %d",&sum,&n) !EOF){ //實際上是!=
extra tokens at end of #include directive [enabled by default]|
#include <stdio.h>#include <stdlib.h.> //每個預處理指令如#include應該單獨占據一行
int main()
{
return 0;
}
E:\chen\dream\ex.c|1|warning: extra tokens at end of #include directive [enabled by default]|
format '%X' expects argument of type 'XXX',
示例一
#include <stdio.h>
int main()
{
int a;
scanf("%d", a); //應該是 &a
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat]|
E:\chen\dream\ex.c|5|warning: 'a' is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 3 second(s)) ===|
||=== Run: Debug in dream (compiler: GNU GCC Compiler) ===|
解釋: 這個是警告,但是該警告往往是不能忽略,初學者一般是忘記了取址&符號
示例二:
#include <stdio.h>
int main(void)
{
int a=3, b=4;
printf("%d%d", a); //兩個%d后面應該有兩個參數,此處只有一個a,缺了b
return 0;
}
E:\chen\dream\ex.c|5|warning: format '%d' expects a matching 'int' argument [-Wformat]|
'gets' is deprecated
這個警告目前可以忽略,是說不鼓勵使用gets
while(gets(str)!=NULL)
warning: 'gets' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
implicit declaration of function 'XXX'
解釋: 缺少函數原型.
如果是調用庫函數,可能沒有包含頭文件,示例沒有加stdio.h.或者寫錯了函數名(例如把scanf寫成Scanf)
如果是自定義的函數,記住調用之前一定要聲明或者定義.
示例一
int main()
{
int a;
scanf("%d", &a);
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|4|warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]|
E:\chen\dream\ex.c|4|warning: incompatible implicit declaration of built-in function 'scanf' [enabled by default]|
||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
||=== Run: Debug in dream (compiler: GNU GCC Compiler) ===|
示例二
#include <stdio.h>
int main(void)
{
int a=3;
print("%d",&a); //函數名寫錯
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]|
obj\Debug\ex.o||In function `main':|
E:\chen\dream\ex.c|5|undefined reference to `print'|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
incompatible type for argument X of 'XXX
#include <math.h>
int main()
{
int a = sqrt("4"); //”4”錯誤
return 0;
}
E:\chen\dream\ex.c|4|error: incompatible type for argument 1 of 'sqrt'|
d:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\math.h|157|note: expected 'double' but argument is of type 'char *'|
E:\chen\dream\ex.c|4|warning: unused variable 'a' [-Wunused-variable]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
解釋:調用函數時實參類型和形參類型不兼容.如示例實參是字符串(指針),而形參是double類型
invalid operands to binary X
#include <stdio.h>
int main(void)
{
int a;
scanf("%d"&a);//漏掉了逗號,把&當成二元運算符了
return 0;
}
E:\chen\dream\ex.c|5|error: invalid operands to binary & (have 'char *' and 'int')|
invalid preprocessing directive 'XXX
#inclede <iostream> //include寫錯了
int main()
{
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c|1|error: invalid preprocessing directive #inclede|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
解釋:預處理指令寫錯了.
invalid suffix "X" on integer constant
#include <stdio.h>
int main()
{
int a = 3, b;
b = 2a; //本意是2*a
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|error: invalid suffix "a" on integer constant|
E:\chen\dream\ex.c|4|warning: variable 'b' set but not used [-Wunused-but-set-variable]|
E:\chen\dream\ex.c|4|warning: unused variable 'a' [-Wunused-variable]|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
解釋: 編譯器把2a看成一個常數,a就是后綴,但是這個后綴不存在. 如3L, 2.5f的L和f就是合法的后綴.示例的錯誤是初學者有時候犯的,應該寫成2*a.
lvalue required as left operand of assignment
解釋:賦值號的左邊應該是左值(一般而言是變量)
#include <stdio.h>
int main(void)
{
int a = 3;
if(a % 3 = 0) //此處實際是==號,誤寫為=
printf("YES");
return 0;
}
E:\chen\dream\ex.c|5|error: lvalue required as left operand of assignment|
missing terminating X character
情況一: missing terminating " character 漏掉了右側的雙引號
示例一
#include <stdio.h>
int main()
{
printf("hello,world\n);//漏掉了右邊的雙引號
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|4|warning: missing terminating " character [enabled by default]|
E:\chen\dream\ex.c|4|error: missing terminating " character|
E:\chen\dream\ex.c|5|error: expected expression before 'return'|
E:\chen\dream\ex.c|6|error: expected ';' before '}' token|
E:\chen\dream\ex.c|6|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
原因: 字符串少了應該用一對雙引號,示例中右邊的雙引號漏掉了
示例二
#include <stdio.h>
int main(void)
{
print("yesn\");
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|4|warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]|
E:\chen\dream\ex.c|4|warning: missing terminating " character [enabled by default]|
E:\chen\dream\ex.c|4|error: missing terminating " character|
E:\chen\dream\ex.c|5|error: expected expression before 'return'|
E:\chen\dream\ex.c|6|error: expected ';' before '}' token|
E:\chen\dream\ex.c|6|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 3 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
這個錯誤是把\n誤寫為 n\, 然后\起轉義作用和雙引號結合,導致編譯器認為少了一個雙引號
情況二、missing terminating ' character
char a='3; //很明顯少了單引號
printf("%d %d %d\n"'a,b,c);//本例單引號實際上是逗號
情況三:missing terminating > character
#include<stdio.h //很明顯少了>
error: missing terminating > character
fatal error: XXX: No such file or directory
寫錯文件名了,例如有寫成stdio,h的。
示例一
#include <studio.h> //文件名寫錯了,stdio.h
int main()
{
int a, b;
scanf("%d%d", &a,&b);
return 0;
}
E:\chen\dream\ex.c|1|fatal error: studio.h: No such file or directory|
示例二 iostream不要用C提交,而是該用C++提交
#include<iostream>
redefinition of ‘XXX’
解釋 XXX重復定義了
原因: (1) 忘了前面已經使用該名字定義標識符了,解決辦法是使用新的名字即可.
(2)很多同學在已有的程序后面再寫新的程序,導致兩個main出現.解決辦法刪掉原來的或者創建一個新文件來寫新程序.
#include <stdio.h>
int main()
{
return 0;
}
int main()
{
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\dream\ex.c|6|error: redefinition of 'main'|
E:\dream\ex.c|2|note: previous definition of 'main' was here|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
subscripted value is neither array nor pointer nor vector
#include <stdio.h>
int main(void)
{
int a[3]={1,2,3}, n=4;
n[2]=1; //a誤寫成n,n不是數組類型,也不是指針,不可以[ ]運算
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|error: subscripted value is neither array nor pointer nor vector|
E:\chen\dream\ex.c|4|warning: variable 'n' set but not used [-Wunused-but-set-variable]|
E:\chen\dream\ex.c|4|warning: unused variable 'a' [-Wunused-variable]|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
suggest parentheses around '&&' within '||'(警告)
#include <stdio.h>
int isLeap(int year)
{
if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
return 1;
else
return 0;
}
int main(void)
{
return 0;
}
E:\chen\dream\ex.c|4|warning: suggest parentheses around '&&' within '||' [-Wparentheses]|
解釋:&&優先級比||高,為了明確此點,你可以加上括號消除這個警告。如下
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
suggest parentheses around assignment used as truth value
#include <stdio.h>
int main()
{
int a = 3;
if(a = 4) // 此處是否為==而不是=
printf("equal\n");
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
解釋: 雖然只是警告, 但是對於大部分初學者來說,往往是把等於”==”誤寫為賦值”=”
statement with no effect(警告)
語句沒有作用
示例一
#include <stdio.h>
int main(void)
{
23; //顯然這條語句沒有任何作用,可以刪除
}
E:\chen\dream\ex.c|4|warning: statement with no effect [-Wunused-value]|
示例二
#include <stdio.h>
int main(void)
{
int n;
n<=10000&&n>=1;//此表達式應該和if,while之類的結合使用,否則不起作用
return 0;
}
E:\chen\dream\ex.c|5|warning: statement with no effect [-Wunused-value]|
stray '\XXX' in program
總體來說是程序不認識的符號。
原因有
(1)由於中文輸入法的緣故,使用了中文的標點符號(全角),比如;,},+,改成英文的標點半角符號就行了(搞不清全角半角的話直接切換到英文輸入環境即可)。
(2)在某個平台下格式不同的文件拿到OJ提交
示例一
#include <stdio.h>
int main()
{
int a = 3;//這里分號是全角的
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|4|error: stray '\357' in program|
E:\chen\dream\ex.c|4|error: stray '\274' in program|
E:\chen\dream\ex.c|4|error: stray '\233' in program|
E:\chen\dream\ex.c|5|error: expected ',' or ';' before 'return'|
E:\chen\dream\ex.c|4|warning: unused variable 'a' [-Wunused-variable]|
E:\chen\dream\ex.c|6|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 5 second(s)) ===|
示例二: int main(void) //注意這里的括號
示例三
v#include <stdio.h> //前面多了一個v,導致#無法識別為預處理指令,#必須在最前面
int main(void)
{
int a#b; //#要么是逗號分成兩個標志符,要么是字母構成一個完整的標識符
printf("no"\n); //\n應該放到雙引號里面
return 0;
}
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\ex.c|1|error: stray '#' in program|
E:\chen\dream\ex.c|1|error: unknown type name 'v'|
E:\chen\dream\ex.c|1|error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token|
E:\chen\dream\ex.c|4|error: stray '#' in program|
E:\chen\dream\ex.c|5|error: stray '\' in program|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
示例四:
rem=x%2;x=x\2; // 除號寫錯了
error: stray '\' in program
too few arguments to function 'XXX'
#include <stdio.h>
int main()
{
int a;
scanf(); //scanf里面沒有參數
return 0;
}
E:\chen\dream\ex.c||In function 'main':|
E:\chen\dream\ex.c|5|error: too few arguments to function 'scanf'|
解釋:調用函數是參數個數少了
‘XXX’ undeclared
解釋 你使用了標識符‘XXX’,但是沒有看到它的定義
原因 (1)你真正忘記定義變量就直接使用了,本例就是這種情況。
(2)寫錯了變量(標識符)的名字,導致前后不一致,還有注意C語言區分大小寫 .解決辦法是保證前后使用同一個標識符
(3)寫錯了關鍵字,C語言的很多關鍵字都是英文單詞,如continue,return,else等等,寫錯了改正過來即可
(4)把數字0誤寫成字母o
(5)使用了頭文件常量,但是沒有#include頭文件。例如使用EOF,NULL要#include <stdio.h>
示例一
#include <stdio.h>
int main()
{
a = 3;
return 0;
}
E:\dream\ex.c 4 error: 'a' undeclared (first use in this function)|
E:\dream\ex.c 4 note: each undeclared identifier is reported only once for each function it appears in
示例二
#include <stdio.h>
int main(void)
{
int a = 3;
if(a % 3 == o) //有趣之處數字0誤寫成字母o
print("yes");
return 0;
}
E:\chen\dream\ex.c|5|error: 'o' undeclared (first use in this function)|
undefined reference to ‘XXX’
解釋:
你調用了’xxx’(例子中是print)函數,但是在目標文件中卻找不到它
原因
1 拼錯了函數的名字,尤其要注意大小寫.例子是printf,漏掉了f
2 真正漏掉了這個函數項目中要添加對應的函數(或文件)
3. 如果出現undefined reference to `WinMain@16' (或main)一般是main的函數名寫錯了,例如誤寫成mian. 還有可能就是程序中沒有main函數.
4. 調用了非標准庫函數,如getch()
示例
#include <stdio.h>
int main()
{
print("hello,world\n");
return 0;
}
編譯信息
In function `main':
E:\dream\ex.c 4 undefined reference to `print'
unknown type name 'fioat'
寫錯了類型名
fioat p(float n); //很明顯應該是float
error: unknown type name 'fioat'
unused variable 'XXX'
#include <stdio.h>
int main()
{
int a = 3; //a在后面沒有被用到
return 0;
}
E:\chen\dream\ex.c|4|warning: unused variable 'a' [-Wunused-variable]|
解釋:這是一個警告,如果一個變量未使用,意味着刪掉它對程序沒有影響.不刪掉也可以,只是多余而已.
||=== Build: Debug in dream (compiler: GNU GCC Compiler) ===|
E:\chen\dream\main.cpp||In function 'SElemType GetTop(SqStack)':|
E:\chen\dream\main.cpp|38|warning: control reaches end of non-void function [-Wreturn-type]|