error C2144: 語法錯誤:“int”的前面應有“;”
C++- error C2144 syntax error : ‘int’ should be preceded by ‘;’
注:我使用VS2010時,遇到的問題。
解決辦法
在某個.h文件里你自定義的某個類最后的“;”,你一定使用的是中文輸入法下的“;”,將它換為英文輸入法,輸入”;“。問題解決。
注意:
中文輸入法下的“;”和英文輸入法下的“;”實在是太像了。
實例
錯誤程序
main.cpp
#include <iostream>
#include "helloworld.h"
int main(void)
{
HelloWorld hello;
hello.say();
while(1){}
return 0;
}
helloworld.h
#ifndef __HELLOWORLD_H_
#define __HELLOWORLD_H_
class HelloWorld{ public: HelloWorld(){}
void say(){ std::cout << "Hello World!" << std::endl; }
};
#endif
編譯失敗:
1> main.cpp : error C2144: 語法錯誤:“int”的前面應有“;”
1>
1>生成失敗。
修改后正確的程序
修改helloworld.h
#ifndef __HELLOWORLD_H_
#define __HELLOWORLD_H_
class HelloWorld{ public: HelloWorld(){}
void say(){ std::cout << "Hello World!" << std::endl; }
};
#endif
編譯成功:
1>生成成功。
注意:
另一種解決辦法,(但是我不推薦使用):在main.cpp的main()函數返回變量
int
前面加上一個“;
”。也可以解決問題。
#include <iostream> #include "helloworld.h" ;int main(void) { HelloWorld hello; hello.say(); while(1){} return 0; }
參考網站:
1. http://stackoverflow.com/questions/11808432/c-error-c2144-syntax-error-int-should-be-preceded-by