简体   繁体   中英

Syntax error in C

 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 specifier-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 (ie 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM