Search code examples
c++data-structuresenums

Can an enum be used as a data structure here? Error: "Redefinition of enumerator"


I am trying to calculate temperature from IMU using its gain and offset, and this is how I'd like to retrieve the gain and offset value, so that I know I get the right value from the right type of IMU:

float gain = IMU_TEMPERATURE_CHARACTERISTICS::GAIN::type1;
float offset = IMU_TEMPERATURE_CHARACTERISTICS::ZERO_ADC_OFFSET::type1;

So I created this data structure

struct IMU_TEMPERATURE_CHARACTERISTICS
{
    enum GAIN
    {
        type1 = 4,
        type2 = 256
    };

    enum ZERO_ADC_OFFSET
    {
        type1 = 15,
        type2 = 20
    };
};

However, my IDE complains

Redefinition of enumerator 'type1' previous definition is here

and obviously about type2 as well

Redefinition of enumerator 'type2' previous definition is here 

which complains about the type1 and type2 defined in ZERO_ADC_OFFSET.

I am wondering how should I change my data structure, or even what data structure I should use in this scenario?


Solution

  • What you are requiring with first snippet of code, which I'll shorten to this

    float gain = imu::gain::type1;
    float offset = imu::offset::type1;
    

    is essentially to have 4 numbers and be able to access them via two levels of naming, each of which has 2 possible alternatives.

    I also suspect that you have been silent about the fact that those 4 numbers are actually 4 constants, in the sense there will be never other values then those 4 in your program.

    If the above is true, then you could be happy with just having nested namespaces:

    namespace imu {
    namespace gain {
    double type1 = 4;
    double type2 = 256;
    }
    namespace offset {
    double type1 = 15;
    double type2 = 20;
    }
    }
    

    which will allow the same syntax you asked for.

    Anyway, whether my hypothesis on your intentions is correct or not, I agree you're putting the cart before the horse.