C 中的语法错误

发布于 2024-11-17 22:34:07 字数 272 浏览 1 评论 0原文

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

上面的代码报告:

error: expected specifier-qualifier-list before ‘state’

使用枚举类型有什么问题?

 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻安 2024-11-24 22:34:07

使用枚举状态或包含typedef枚举状态

在 C 中,枚举标记与标识符(变量、函数或 typedef)位于不同的命名空间中。

Use enum state or include typedef enum state state.

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

一生独一 2024-11-24 22:34:07

尝试

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

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

稍微链接到此常见问题解答条目。这是关于命名空间的讨论。

有四种不同类型的命名空间,用于:

  • 标签(即 goto 目标);
  • 标签(结构名称、联合名称、
    和枚举;这三个不是
    即使他们分开
    理论上可以);
  • 结构/工会成员(一名
    每个结构或联合的命名空间);
  • 其他一切(功能,
    变量、typedef 名称、枚举
    常数),称为“普通
    标准中的“标识符”。

编辑

由于OP要求提供示例..

struct foo {
    int bar;
    int foo;
};

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

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;
};
一口甜 2024-11-24 22:34:07

如果你用c++就可以了;

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

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;
燃情 2024-11-24 22:34:07

你需要

typedef struct {
    enum state st;

You need

typedef struct {
    enum state st;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文