c - 如何在我的结构中使用类型定义的枚举?

标签 c enums

嗨,我在一个日志系统上,想要创建一个保存记录器内部状态的结构:

#include "logger.h"
#include "main.h"
/*
 * Variables
 */

/*State of Logging*/
struct {
    bool logOn;
    static enum eLogLevel outputLevel[7];
} sLogStruct;

static struct sLogStruct gLogData;

void Log(enum eLogSubSystem sys, enum eLogLevel level, char * msg)
{

}

我在 logger.h 文件中定义了 eLogLevel 枚举:

#ifndef LOGGER_H_
#define LOGGER_H_

#include "globaldefinitions.h"

/*
 * Logger data types
 */

typedef enum eLogLevel {none, information_only, debugging, warning, error, critical};
typedef enum eLogSubSystem {coresystem, heartrate, temperature, accelerometer, communication, flash, firmwareUpdate};
/*
 * Public functions
 */

void Log(enum eLogSubSystem sys, enum eLogLevel level, char * msg);
void LogWithNum(enum eLogSubSystem sys, enum eLogLevel level, char * msg, int number);
void LogSetOutputLevel(enum eLogSubSystem sys, enum eLogLevel level);
void LogGlobalOn(void);
void LogGlobalOff(void);
void LogVersion (em_fw_version_t version);


#endif /* LOGGER_H_ */

但是,我的编译器提示:

../Logging/logger.c:18:2: error: expected specifier-qualifier-list before 'static'
  static enum eLogLevel outputLevel[7];

我不知道如何解决这个问题,我猜它很简单,但我不知道为什么它不接受 logger.h 文件中的 typedef 枚举 eLogLevel。 你能帮我理解一下吗?

最佳答案

您的原始代码存在不同的问题。

  • 在 C 语言中,struct 的成员不能是静态的。如果需要,请使用 C++
  • 您的typedef不正确。他们应该写:

    typedef enum {none, information_only, debugging, warning, error, critical} eLogLevel ;
    

    struct {
        bool logOn;
        eLogLevel outputLevel[7];
    } sLogStruct;
    

    typedef struct {
        bool logOn;
        eLogLevel outputLevel[7];
    } SLogStruct;
    SLogStruct sLogStruct;
    

关于c - 如何在我的结构中使用类型定义的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39513811/

相关文章:

c++ - C++ 枚举使用起来比整数慢吗?

ios - 具有动态 alpha 值的枚举颜色

ios - 具有关联值的枚举的数据模型

c - 我是否误解了 GCC 中的 __attribute__ ((packed)) ?

c - 以编程方式处理 ctrl +d?

c - 最后修改字段和下载文件

c - Emacs C 模式中的自动注释结尾大括号

c - stdout 重定向更改输出

java - 在测试驱动程序中初始化 ArrayList

ios - 实例成员不能用于嵌套类型的实例?