views:

1142

answers:

8

I'm compiling a simple .c in visual c++ with Compile as C Code (/TC) and i get this compiler error error C2143: syntax error : missing ';' before 'type' on a line that calls for a simple struct

 struct foo test;

same goes for using the typedef of the struct. error C2275: 'FOO' : illegal use of this type as an expression

+1  A: 

Did you accidentally omit a semicolon on a previous line? If the previous line is an #include, you might have to look elsewhere for the missing semicolon.

Edit: If the rest of your code is valid C++, then there probably isn't enough information to determine what the problem is. Perhaps you could post your code to a pastebin so we can see the whole thing.

Ideally, in the process of making it smaller to post, it will suddenly start working and you'll then have discovered the problem!

Greg Hewgill
A: 

nope, the code is valid, it compiles as a c++ but not as c

CiNN
A: 

How is your structure type defined? There are two ways to do it:

// This will define a typedef for S1, in both C and in C++
typedef struct {
     int data;
     int text;
} S1;

// This will define a typedef for S2 ONLY in C++, will create error in C.
struct S2 {
     int data;
     int text; 
};
17 of 26
A: 

the S1 version

CiNN
+1  A: 

Because you've already made a typedef for the struct (because you used the 's1' version), you should write:

foo test;

rather than

struct foo test;

That will work in both C and C++

Will Dean
A: 

i have tried both ways and still have that error...

CiNN
A: 

C2143 basically says that the compiler got a token that it thinks is illegal in the current context. One of the implications of this error is that the actual problem may exist before the line that triggers the compiler error. As Greg said I think we need to see more of your code to diagnose this problem.

I'm also not sure why you think the fact that this is valid C++ code is helpful when attempting to figure out why it doesn't compile as C? C++ is (largely) a superset of C so there's any number of reasons why valid C++ code might not be syntactically correct C code, not least that C++ treats structs as classes!

Andrew O'Reilly
+2  A: 

okay found out what was the problem... forgot that in C you have to declare all your variables before any code.

CiNN
seems like I had the same problem.