开发者

Syntax error in C

开发者 https://www.devze.com 2023-03-16 17:15 出处:网络
5 enum state {ST_BEFORE_KEY, ST_IN_KEY, ST_BEFORE_VALUE, ST_TERM, ST_ERR}; 6 7 typedef struct { 8state st;
 5 enum state {ST_BEFORE_KEY, ST_IN_KEY, ST_BEFORE_VALUE, ST_TERM, ST_ERR};
  6 
  7 typedef struct {
  8   state st;
      ...

The above code reports :

error: expected specifie开发者_运维百科r-qualifier-list before ‘state’

What's wrong here in using enum type?


Use enum state or include typedef enum state state.

Enumeration tags are in a different namespace in C than identifiers (variables, functions or typedefs).


Try

enum state {ST_BEFORE_KEY, ST_IN_KEY, ST_BEFORE_VALUE, ST_TERM, ST_ERR};

typedef struct {
    enum state st;
    ...
};

Marginally linked to this FAQ entry. And here's a discussion on namespaces.

There are four different kinds of namespaces, for:

  • labels (i.e. goto targets);
  • tags (names of structures, unions, and enumerations; these three aren't separate even though they theoretically could be);
  • structure/union members (one namespace per structure or union); and
  • everything else (functions, variables, typedef names, enumeration constants), termed ``ordinary identifiers'' by the Standard.

EDIT

Since the OP is asking for an example..

struct foo {
    int bar;
    int foo;
};

struct bar {
    int foo;
    struct foo bar;
};


If you use c++, it is ok;

on c ( not c++) you should write like this.

5 enum state {ST_BEFORE_KEY, ST_IN_KEY, ST_BEFORE_VALUE, ST_TERM, ST_ERR};
6 
7 typedef struct {
8   enum state st;


You need

typedef struct {
    enum state st;
0

精彩评论

暂无评论...
验证码 换一张
取 消