C2143: 在某類型前缺分號--VC裏面的各種問題,應該首先查閱msdn

今天將P_Demo.cpp=>P_Demo.c。

 

裏面某函數體定義如下:

就報如下錯誤

p_demo.c(68) : error C2275: 'TSint32' : illegal use of this type as an expression
        讓我去../NY_ipp.h(60) : see declaration of 'TSint32'
p_demo.c(68) : error C2146: syntax error : missing ';' before identifier 'nImageWidth'
p_demo.c(68) : error C2065: 'nImageWidth' : undeclared identifier

而NY_ipp.h中定義TSint32如下:

    typedef signed int TSint32;

這顯然是沒有任何問題的。去查錯誤類型C2275,找不到任何信息。

 

以爲是程序識別不了我定義的TSint32。沒辦法,只好將TSint32替換成signed int

 

結果它仍然報錯

p_demo.c(68) : error C2143: syntax error : missing ';' before 'type'

 

再去msdn中去查該錯誤,這下就有戲了。MSDN解釋如下:

PRB: Executable Code Between Declarations Causes C2143 or C2144
ID: Q58559

 

SYMPTOMS
In Microsoft C, compiler errors C2143 and C2144 are defined as follows:

C2143: syntax error : missing 'token1' before 'token2'

C2144: syntax error : missing 'token' before type 'type'


CAUSE
You may receive this error message if your program places executable code before a data declaration, an acceptable practice in Kernighan-and-Ritchie C. This practice has been outlawed in later versions of the ANSI drafts.

This error message will normally occur if a required closing curly brace (}), right parenthesis [)], or semicolon (;) is missing. 聲明語句放在執行語句後面了,雖然K&R C允許,但是ANSI C不允許!

 

RESOLUTION
Placing all data declarations before all executable code corrects the programming error.


void main( )
{
   int i;
   printf( "Hello world!/n" );
   {
      int j;
   }
}
NOTE: In the C++ language, it is legal to declare data within a block of executable code.

注意:C++中允許在執行語句模塊(即一對大括號)中聲明數據;

 

MORE INFORMATION
The following code demonstrates this error message:

Sample Code


Compiling this code with a version of Microsoft C prior to C/C++ 7.0 will return the

following error message:
C2144: syntax error : missing ';' before type 'int'


C/C++ version 7.0 and Visual C/C++ issue the following error:
C2143: syntax error : missing ';' before 'type'

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章