Alex Hoffmann
Alex Hoffmann

Reputation: 367

Enum error, variable undeclared

So I have some code that is throwing me an error and I am not sure why. I am using a macro to generate myself a string array and an enum that I can use to access the string array in a intuitive way. My compiler is complaining about my macro but the rest of my code seems to work as if the macro successfully created the enum which has me quite confused.

The macro is as follow

#define FOR_EACH_PHASE(PHASE)       \
            PHASE(init)         \
            PHASE(framerate)    \
            PHASE(priority)     \
            PHASE(time)         \
            PHASE(powersave)    \
            PHASE(performance)  \
            PHASE(response)

#define GENERATE_ENUM(ENUM) AI_phase_##ENUM,
#define GENERATE_STRING(STRING) "AI_phase_"#STRING,


typedef enum PHASE_ENUM PHASE_ENUM_t;
enum PHASE_ENUM {
    FOR_EACH_PHASE(GENERATE_ENUM)
    END
};

static const char* PHASE_STRINGS[] = {
    FOR_EACH_PHASE(GENERATE_STRING)
};

Lines such as these seems to compile without error

struct phase_profile* set_defaults;
set_defaults = AI_phases_get_name(PHASE_STRINGS[AI_phase_framerate]);

But the compiler is giving me the following error for each line of my FOR_EACH_PHASE macro

error: 'performance' undeclared (first use in this function)

Any ideas from anyone that knows more about this than me?

Cheers

Upvotes: 0

Views: 724

Answers (1)

Triantafillos Paradas
Triantafillos Paradas

Reputation: 46

I don't have the required reputation to comment so I am posting this as an answer.

I believe you need this:

https://www.codeproject.com/Articles/32000/Improving-C-Enums-Adding-Serialization-Inheritance

I use it for some years now and it is fine!

Upvotes: 2

Related Questions