简体   繁体   English

C 中的语法错误

[英]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 .使用enum state或包括typedef enum state state

Enumeration tags are in a different namespace in C than identifiers (variables, functions or typedefs).枚举标签在 C 中的命名空间与标识符(变量、函数或类型定义)不同。

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.其他一切(函数、变量、 typedef 名称、枚举常量),被标准称为“普通标识符”。

EDIT编辑

Since the OP is asking for an example..由于OP要求举个例子..

struct foo {
    int bar;
    int foo;
};

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

If you use c++, it is ok;如果你用c++就可以了;

on c ( not c++) you should write like this.在 c(不是 c++)上,你应该这样写。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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