2

I have an enum:

enum eOperandType
{
    Int8,
    Int16,
    Int32,
    Float,
    Double
};

and a member function of the class 'Double'

eOperandType    Double::getType(void) const
{
        return (eOperandType::Double);
}

and it gives me a compiler warning about using enumeration in nested name specifier.

I also switched the return line to: return (Double); but then it just gave me an error regarding an expected expression.

How do i resolve this?


edit: changing the line to return (::Double); did fix both the warning and the error. Could someone explain why this fixes it?

Theo Walton
  • 1,085
  • 9
  • 24
  • Why do you have the unnecessary parentheses around your return value? Why do you have `getType(void)`; that should be `getType()`. – Justin Jan 24 '18 at 20:25
  • `return (Double);` probably fails because it looks like a cast; `Double` there would be referring to your class `Double` – Justin Jan 24 '18 at 20:27
  • 1
    @Justin White spaces and new lines can also be unnecessary! – Zingam Jan 24 '18 at 20:27
  • @TheoWalton Try not using `eOperandType::` before the enumeration value. – Zingam Jan 24 '18 at 20:28
  • 2
    @Zingam Yes, but `return (...)` easily leads to bugs in some cases. Thus I claim it's best to avoid it entirely. And it's completely unneeded, even for readability – Justin Jan 24 '18 at 20:28
  • 2
    You might want to check out [`enum class`](https://stackoverflow.com/q/18335861/1896169) – Justin Jan 24 '18 at 20:29
  • 5
    Your class and the enum value are both called `Double` – Mihayl Jan 24 '18 at 20:30
  • @Justin You may be right but I once read the opposite argumentation to your claim. My point is - the original question is not about style. – Zingam Jan 24 '18 at 20:33
  • 1
    Please note that *"changing the line to `return (::Double);`"* doesn't really fix your code: https://ideone.com/PQFLJX You should use an enum class, a scoped enum or better, change the names. – Bob__ Jan 24 '18 at 20:50

2 Answers2

9

Prior to C++11, you were not allowed to refer to enumerators as enum-name::enumerator. So eOperandType::Double is invalid if you're using an old compiler or haven't enabled C++11 mode. If you're using gcc or clang, pass the -std=c++11 flag to the compiler.

If that's not an option, then change return (Double); to return ::Double;, or return (::Double); if you're fond of unnecessary parentheses. Without the ::, Double refers to the name of the enclosing class. By adding the scope resolution operator, you're telling the compiler you're referring to Double in the global namespace, i.e. the enumerator.

The third option is to not give your class the same name as enumerators.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • I don't think that changing the line to `return (::Double);`" would be enough: https://ideone.com/PQFLJX , but OP could limit the scope of the enum using a namespace: https://ideone.com/pAqw8b – Bob__ Jan 24 '18 at 20:59
  • 1
    @Bob__ The error in your example has nothing to do with the return statement, it's in the declaration of `a` within `main`. Since the code is using the same name for the class and enumerator, you must use an [elaborated type specifier](http://en.cppreference.com/w/cpp/language/elaborated_type_specifier), `class Double a;` - https://ideone.com/Vss3OI – Praetorian Jan 24 '18 at 21:09
  • You are right and that's just a further reason to use different names. – Bob__ Jan 24 '18 at 21:16
0

If you want to used scoped enumerations you need to make the enum:

enum struct eOperandType

(or"enum class")

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23