Iamafox
Iamafox

Reputation: 28

How can i use a typedeffed enum in my struct?

Hi i am on a logging system and want to make a struct that holds the internal state of my logger:

#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)
{

}

I defined the eLogLevel enumeration in the logger.h file:

#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_ */

However, my compiler complains that:

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

I cant figure out how to solve this, i guess its trivial, but i dont know why it doesnt accept my typedef enum eLogLevel from the logger.h file. Can you please help me understand?

Upvotes: 0

Views: 228

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148890

There are different problems in your original code.

  • in C, a member of a struct cannot be static. Use C++ if you want that
  • your typedef are not correct. They should write:

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

    and

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

    or

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

Upvotes: 1

Iamafox
Iamafox

Reputation: 28

Ok i modified it:

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

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

static sLogStruct gLogData;

sLogStruct * LogInternalState(void){
#if PRODUCTION
    #error "Internal state of logging protected!"
#else
    return &gLogData;
#endif /*PRODUCTION*/

}

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

}

It works like this now. I keep the struct itself static but not any members of it. Thanks for the link RomanHocke.

However, i also had to change the struct declaration part

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

static struct sLogStruct gLogData;

to a typedef struct type of thing:

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

static sLogStruct gLogData;

otherwise the compiler would say:

../Logging/logger.c:21:26: error: storage size of 'gLogData' isn't known
 static struct sLogStruct gLogData;

Actually i thought the typedef struct and struct definitions where sort of the same thing...

Upvotes: 0

Related Questions