Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is enum a good data structure here?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

Some sample code is highly appreciated. Thanks!

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading