2

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 ?

timrau
  • 22,578
  • 4
  • 51
  • 64
Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76
  • possible duplicate of [namespaces for enum types - best practices](http://stackoverflow.com/questions/482745/namespaces-for-enum-types-best-practices) – timrau Jun 06 '14 at 07:53
  • This use C++11 solution, won't work, because i'm not using this type compilator, so there's no duplication. – Evans Belloeil Jun 06 '14 at 07:58
  • Putting the enums in different files won't help... they need to be in different namespaces or classes to avoid the identifier clash. You can create e.g. `class In { public: enum Enum { X, Y }; In(Enum e) : e_(e) { } operator Enum() const { return e_; } };` etc. then refer unambiguously to `In::X`. Separately, best to avoid multi-letter all-uppercase identifiers - they're traditionally used only by the preprocessor, and if you have clashes with the preprocessor they can be very confusing and aggravating to debug. – Tony Delroy Jun 06 '14 at 08:02
  • Thanks, this seem realy complicated just for 1 variable, is there a way to to it in .h file without create a class ? – Evans Belloeil Jun 06 '14 at 08:04
  • How about renaming to `MODEMANAGEMENT_OUT` and `MODEMANAGEMENT_IN`. This way is simple and less error prone. – 101010 Jun 06 '14 at 08:04
  • You either use the class, or you use `namespace In { enum Enum { X, Y }; }` but then the type name is more verbose: `In::Enum`. – Tony Delroy Jun 06 '14 at 08:05
  • @40two It is possible, but not really clean :/ – Evans Belloeil Jun 06 '14 at 08:07
  • @TonyD didn't work, tells me expected a declaration for the namespace In – Evans Belloeil Jun 06 '14 at 08:10
  • @EvansBelloeil [here's proof it does work on ideone.com](http://ideone.com/54Z7mU) - what did you do? – Tony Delroy Jun 06 '14 at 08:15
  • @TonyD namespace IN { enum Enum { MODEMANAGEMENT, SYSTEMMONITOR};} – Evans Belloeil Jun 06 '14 at 08:16
  • And? There's nothing wrong with that. You haven't still got a conflicting `enum IN` defined earlier? Just append your current code to the question so I can have a proper look if you can't work it out.... – Tony Delroy Jun 06 '14 at 08:22
  • No conflict with another enum, I don't understand why it doesn't work. But namespace In is underline in red with expected a declaration – Evans Belloeil Jun 06 '14 at 08:25
  • That might mean the line before `namespace` is missing a semicolon, or you're trying to put the namespace inside a function or class? Again, post your current code if you want further help.... – Tony Delroy Jun 06 '14 at 08:37
  • @TonyD Someone in the answer told me it was impossible to do in a structure. – Evans Belloeil Jun 06 '14 at 08:40
  • @EvansBelloeil: the answer says "they can't be placed at the ***same*** structure" - that's true... you can place each `enum` into a ***separate*** `class`, `struct` or `namespace`. – Tony Delroy Jun 06 '14 at 08:52
  • @TonyD Yes but I want them place in the same structure – Evans Belloeil Jun 06 '14 at 08:56
  • Then change the names... your pick. – Tony Delroy Jun 06 '14 at 08:59
  • "Yes but I want them place in the same structure" You already know by now that you can't place enumerators with same name in one structure. The fact that you want it is irrelevant. It can't be done using C-style enums. That's it. End of the ride. – Kuba hasn't forgotten Monica Jun 07 '14 at 03:22

1 Answers1

3

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

ttoth
  • 76
  • 3