program story

형식 내부에 열거 형 사용-컴파일러 경고 C4482 C ++

inputbox 2020. 11. 17. 08:04
반응형

형식 내부에 열거 형 사용-컴파일러 경고 C4482 C ++


내 클래스 중 하나의 메서드 내에서 열거 형의 정규화 된 이름을 사용하고 있습니다. 하지만 "경고 C4482 : 비표준 확장이 사용됨 : enum 'Foo'used in Qualified name"이라는 컴파일러 경고가 표시됩니다 . C ++에서 정규화 된 이름없이 열거 형을 사용해야합니까? 그러나 IMO, 그것은 추악 해 보입니다.

이견있는 사람?


예, 열거 형은 새로운 "네임 스페이스"를 만들지 않습니다. 열거 형의 값은 주변 범위에서 직접 사용할 수 있습니다. 따라서 다음을 얻을 수 있습니다.

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

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

깨끗하게하려면 다음을 교체하십시오.

enum Fruit {

    ORANGE = 0,
    BANANA = 1

};

namespace Fruit {

    enum { //no enum name needed

        ORANGE = 0,
        BANANA = 1

    };

};

...

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

sth가 질문에 대답하지만 항상 열거 형을 사용하는 방법은 다루지 않았습니다. 숫자에 대한 이름이 다소 많더라도 항상 특정 값만 가질 수있는 유형을 정의하는 데 사용했습니다.

열거 형이 클래스의 일부인 경우 소비자가 열거 형 참조를 명확하게 식별하는 데 도움이됩니다.

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

그러면 소비자는 열거 형의 인스턴스를 선언하고, 매개 변수로 전달하고, 유형 중 하나를 참조 할 때이를 한정 할 수 있습니다.

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

때로는 클래스 외부의 열거 형 또는 동일한 클래스의 두 열거 형을 원하며 Poy가 가진 것과 같은 작업을 수행 할 수 있습니다. 하지만 열거 형 유형을 참조 할 수 없으므로 이름을 지정하십시오.

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

이제 열거 형과 값을 사용하면 다음과 같이 작동합니다.

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

if( firstColor == secondColor )
....

이제 같은 이름을 가진 다른 열거 형이 있으면 항상 어떤 유형인지 정규화됩니다. 그러면 gamblor가 요구하는 것을 처리 할 수 ​​있습니다.

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

예. 개념적으로 enum은 유형과 해당 유형의 가능한 값을 정의합니다. 자연스러워 보이지만 정의 enum foo { bar, baz };하고 참조하는 foo::baz것은를 참조하는 것과 같습니다 int::1.



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

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.


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;
}

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.


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 )

참고URL : https://stackoverflow.com/questions/514194/using-enum-inside-types-compiler-warning-c4482-c

반응형