枚举问题"没有命名类型"

ant*_*009 5 c++ enums

g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Run Code Online (Sandbox Code Playgroud)

我遇到了问题,我似乎发现了这个错误.

文件statemachine.h

#ifndef STATEMACHINE_H_INCLUDED
#define STATEMACHINE_H_INCLUDED

#include "port.h"

enum state {
    ST_UNINITIALIZED = 0x01,
    ST_INITIALIZED   = 0x02,
    ST_OPENED        = 0x03,
    ST_UNBLOCKED     = 0x04,
    ST_DISPOSED      = 0x05
};

void state_machine(event evt, port_t *port);

#endif /* STATEMACHINE_H_INCLUDED */
Run Code Online (Sandbox Code Playgroud)

文件port.h

#ifndef PORT_H_INCLUDED
#define PORT_H_INCLUDED

#include <stdio.h>

#include "statemachine.h"

struct port_t {
    state current_state; /* Error 'state does not name a type */
    .
    .
};
#endif /* PORT_H_INCLUDED */
Run Code Online (Sandbox Code Playgroud)

非常感谢任何建议,

Nic*_*ick 7

可能是你在"statemachine.h"中的"port.h"和"port.h"中包含"statemachine.h"吗?

尝试删除该行:

#include "port.h"
Run Code Online (Sandbox Code Playgroud)

从文件"statemachine.h"

编辑(根据Daniel的评论如下):

然后你需要转发声明你的port_t类型如下:

...
    ST_DISPOSED       = 0x05
};

struct port_t;

void state_machine(event evt, port_t *port);
...
Run Code Online (Sandbox Code Playgroud)

  • 只是向前声明,它只是一个指针? (2认同)
  • @ ant2009,如果你打算在你的文件中使用`port_t*`,那么在你的`state_machine()`函数中使用它之前,只需要执行一个前向声明,`class port_t;`.这将解决问题.请注意,您将无法在`statemachine.h`文件中声明`port_t`的任何对象. (2认同)