6

I declared an enum (called Direction) in a header file:

enum Direction {LEFT, RIGHT};

Then, i have a constructor that takes a Direction value and set it for another Direction variable (stringDirection).

bool InformationWidget::move(Direction direction){
    stringDirection=direction;

return true;
}

And finally, i have an if statement that checks the value of it:

if (stringDirection == Direction::RIGHT)

This is where i'm getting the error, at that if statement.. any ideas? I tried looking around at previous threads but didn't find anything useful.

Thanks!

EDIT:

Here are my files:

Widget.h

enum class Direction {LEFT, RIGHT};

class Widget {
public:
     virtual bool...
     ...
};

information.h

class InformationWidget: public Widget {
public:
    ...
    Direction stringDirection;
    ...
};

information.cpp

void InformationWidget::show(){

...

if (stringDirection == LEFT) {
... }
}
Gambit2007
  • 3,260
  • 13
  • 46
  • 86

1 Answers1

12

Declare your enum as enum class or enum struct if you want a scoped enum, i.e. to require the enumerators to be prefixed Direction:::

enum class Direction {LEFT, RIGHT};

Otherwise if you want an old C-style enum then you need to omit the namespace qualifier Direction:: that the error message is complaining about:

if (stringDirection == RIGHT)

Notice that in the latter case the enumerator identifiers will be placed in the global namespace, which might cause them to clash with other identifiers.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • Thanks for your reply! I was aware of these solutions but both of them don't work. When i try the first one (adding 'class') it still gives me the same error. When i try the second one, it only gives an error for the RIGHT part of the if statement, saying "symbol 'RIGHT' could not be resolved". – Gambit2007 Oct 28 '15 at 23:50
  • Then you're doing something wrong somewhere else and need to provide an [MCVE](http://stackoverflow.com/help/mcve). – Emil Laine Oct 28 '15 at 23:59
  • I really don't know what's the problem here.. i declared the enum in header file, included it in a .cpp file (which implements methods from a purely abstract base class if it matters) and it's just not working.. – Gambit2007 Oct 29 '15 at 00:19
  • Sorry, can't help without a reproducible example. (other than guess) – Emil Laine Oct 29 '15 at 00:20
  • Judging by the snippets you added, you haven't included the header and you haven't specified the name of the scoped enum in front of the enumerator. – Emil Laine Oct 29 '15 at 00:38
  • widget.h is included in both information.h and information.cpp, sorry for not including that. and what do you mean? 'Direction' is name of the enumerator, like specified. – Gambit2007 Oct 29 '15 at 00:44
  • You did not write `Direction::LEFT` which is needed since you declared `Direction` as `enum class`. – Emil Laine Oct 29 '15 at 00:45
  • Even when i do it still gives me the error of "Direction is not a class or namespace". I don't know if it matters, but when i declare the enum it gives me a warning "scoped enums only available with -std=c++11...". Regardless, even if i omit the word "class" from the enum declaration, and use "if (stringDirection==LEFT)" it doesn't work.. – Gambit2007 Oct 29 '15 at 00:53
  • Yes, `enum class` is only available since C++11 so you should use `-std=c++11` to tell your compiler to compile as C++11. Or preferably C++14 while at it. Compile warnings are there _for a reason_. (Also that should've been a compile _error_, please check your compile options that they're sensible, e.g. `-pedantic-errors`.) – Emil Laine Oct 29 '15 at 00:57
  • and how would i go about doing that? sorry i have no idea.. – Gambit2007 Oct 29 '15 at 01:00
  • That depends on your build environment and thus is too broad to discuss here. I'm sure Google and/or Stack Overflow and/or your compiler/IDE docs will have the answer for that. – Emil Laine Oct 29 '15 at 01:02
  • Yeah, found it! many thanks for your help! – Gambit2007 Oct 29 '15 at 01:03