Using enum inside types - Compiler warning C4482 C++

C++EnumsCompiler Warnings

C++ Problem Overview


I am using fully qualified name of the enum inside a method in one of my class. But I am getting compiler warning which says "warning C4482: nonstandard extension used: enum 'Foo' used in qualified name". In C++, do we need to use enums without the qualified name? But IMO, that looks ugly.

Any thoughts?

C++ Solutions


Solution 1 - C++

Yes, enums don't create a new "namespace", the values in the enum are directly available in the surrounding scope. So you get:

enum sample {
  SAMPLE_ONE = 1,
  SAMPLE_TWO = 2
};

int main() {
  std::cout << "one = " << SAMPLE_ONE << std::endl;
  return 0;
}

Solution 2 - C++

To make it clean, replace:

enum Fruit {

    ORANGE = 0,
    BANANA = 1

};

with

namespace Fruit {

    enum { //no enum name needed

        ORANGE = 0,
        BANANA = 1

    };

};

...

int f = Fruit::BANANA; //No warning

Solution 3 - C++

While sth does answer the question, it didn't address how I've always used enums. Even though they're just more or less names for numbers, I've always used them to define types that can only have certain values.

If the enum is part of the class, then that helps consumers clearly identify an enum reference:

class Apple {
  enum Variety {
    Gala,
    GoldenDelicious,
    GrannySmith,
    Fuji
  }
  ...
};

Then consumers would be able declare instances of the enum, pass as parameters, and qualify them when referencing one of the types.

unsigned int GetCountOfApples( Apple::Variety appleVariety );
...
fujiCnt = GetCountOfApples( Apple::Fuji );

Sometimes you want an enum outside of a class or two enums in the same class, and you can do something like what Poy had. You won't be able to reference the enum type though, so just name it.

namespace Color {
  enum ColorEnum {
    Blue,
    Red,
    Black
  };

Now using the enum and values would work like:

Color::ColorEnum firstColor = Color::Blue;
Color::ColorEnum secondColor = Color::Red;

if( firstColor == secondColor )
....

Now if there happens to be different enums with the same name in them, they will always be qualified with what type they are. Then you could handle what gamblor is asking about.

BananaColorEnum banCol = BananaColor::Yellow;
TomatoColorEnum tomCol = TomatoColor::Yellow;


Solution 4 - C++

Yes. Conceptually enum defines a type, and the possible values of that type. Even though it seems natural, to define enum foo { bar, baz }; and then refer to foo::baz is the same as referring to int::1.

Solution 5 - C++


namespace Company
{
typedef int Value;
enum
{
Microsoft= 0,
APPLE = 1,
};
};




namespace Fruit
{
typedef int Value;
enum
{
ORANGE = 0,
BANANA = 1,
APPLE = 2,
};
};




...




Fruit::Value f = Fruit::BANANA; //No warning
Company::Value f = Company::APPLE; //is different value then Fruit::APPLE

Fruit::Value f = Fruit::BANANA; //No warning Company::Value f = Company::APPLE; //is different value then Fruit::APPLE

This works on GCC and MS compiler and Mac. And the advantage is that you can use namespace operator and pass conflicts. The little disadvantage is that instead of Fruit, you have to write Fruit::Value. it is more useful in large project when you don't know what enums are in other class.

If it is possible to use C++11 instead, it is much more simple, because the enum::namespace syntax is then possible.

Solution 6 - C++

The cleanest way I've found to do this is defining the enum as such

namespace Samples
{
    enum Value
    {
        Sample1,
        Sample2,
        Sample3
    };
}
typedef Samples::Value Sample;

Then in function and variable definitions you can use the typedef:

void Function(Sample eSample);
Sample m_eSample;

And in your .cpp file you can use the namespace to assign variables:

void Function(Sample eSample)
{
    m_eSample = Samples::Sample1;
    eSample = Samples::Sample2;
}

Solution 7 - C++

Personally, I think this is a compiler bug. I've been using C++ for lots of time. Sadly, no sample code in OP. The interpretation of an enum by the Java people was actually correct iMO. Mine, was like this ...

class Foo {
    enum tMyEnum { eFirstVal = 0, eSecondVal = 1};
    // ...
    tMyEnum m_myVal;
};
void Foo::MyMethod() {
    if(m_myVal == tMyEnum::eFirstVal) {
//  ...
    }
}

I also tried, Foo::tMyEnum::eFirstVal. Without the qualifiers, everything compiled.

Solution 8 - C++

I had the same problem and I'm not using C++ 11 yet. I much prefer fully qualified namespaces myself too.

I disabled this particular warning. I'm sure people will dislike the idea but some may be thankful..

#pragma warning( disable : 4482 )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNavaneeth K NView Question on Stackoverflow
Solution 1 - C++sthView Answer on Stackoverflow
Solution 2 - C++PoyView Answer on Stackoverflow
Solution 3 - C++jmcView Answer on Stackoverflow
Solution 4 - C++Rob KView Answer on Stackoverflow
Solution 5 - C++Tomas KubesView Answer on Stackoverflow
Solution 6 - C++petejamdView Answer on Stackoverflow
Solution 7 - C++georgejoView Answer on Stackoverflow
Solution 8 - C++SparkyNZView Answer on Stackoverflow