Question

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

Was it helpful?

Solution

I forgot that in C you have to declare all your variables before any code.

OTHER TIPS

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!

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++

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; 
};

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top