Evans Belloeil
Evans Belloeil

Reputation: 2503

Enum in structure

My question is not really hard but I can't find a proper answer on the web to my problem.

I define a .h file that contains a struct and declare some enum in it. I want to use that struct in another .cpp file. But the compilation return me error.

Here's my code variableStruct.h file :

struct variableSupervision {
    std::string Donnees;

    enum TYPE
    {
      ENUM,
      INT,
      FLOAT,
      BOOL
    };

    enum UNITE
    {
      ETAT,
      A,
      V,
      TBC,
      DEGREEE
    };

    enum IN
    {
      SYSTEMMONITOR,
      MODEMANAGEMENT
    };

    enum OUT
    {
      SLIDECONTROL,
      MODEMANAGEMENT,
      HMICONTROL,
      MOTORCONTROL
    };


    enum GET {X};

    std::string Commentaire ;
};

The error is : redeclaration Of MODEMANAGEMENT. I don't understand why, because they are in two different enum. Should I create different separate file for each enum ?

Upvotes: 2

Views: 249

Answers (1)

ttoth
ttoth

Reputation: 76

Since C++11 you can use enum class instead of enum to solve this problem.

If you can't use C++11 for some reason you should prefix your values like this:

enum IN
{
  IN_SYSTEMMONITOR,
  IN_MODEMANAGEMENT
};

enum OUT
{
  OUT_SLIDECONTROL,
  OUT_MODEMANAGEMENT,
  OUT_HMICONTROL,
  OUT_MOTORCONTROL
};

or they can't be placed at the same structure, so you have to move the declarations to different namespaces. (EDIT: or different class / structure as it said below.)

Upvotes: 3

Related Questions