Strange visual studio 2008 C++ compiler error

2019-08-29 11:20发布

问题:

I have three lines of code:

 //int pi;
 activation->structSize = sizeof(rmsActivationT);
 int pi; //program wont compile with this here

every time I uncomment the second int pi and comment the first int pi I get this error: syntax error : missing ';' before 'type'. When i uncomment this first int pi and comment the second int pi, my compiler doesn't complain anymore. This error has been bothering me for almost a full day now any ideas would be great.

Thanks

Visual studios 2008 Windows XP 32 bit

回答1:

Are you, perhaps, compiling the code as C instead of C++? C (prior to C99, which Visual Studio doesn't support) required that all definitions in a block precede any other statements.



回答2:

I had the same problem.

The compilation errors were:

*main.cpp(325): error C2601: 'FLAG' : local function definitions are illegal

main.cpp(323): this line contains a '{' which has not yet been matched

main.cpp(326): fatal error C1075: end of file found before the left brace '{' at 'main.cpp(323)' was matched*

But there was nothing wrong with my code. I counted all brackets and the number matched. There weren't any function inside another function.

I solved it by removing all "//" comments from the source code. It seems that the reason for that is bad line formatting which causes the compiler to miss a line break, so the line after a comment is treated as a comment as well.

For example:

// This is a comment

This_is_a_line;

is treated as:

// This is a comment This_is_a_line;