How can i inside enum in class



  • Hellow, how can i inside enum in class?
    I wanna that i can called enum item throught class:
    @ClassName::EnumElement@
    If i declare enum outside class, for example
    @
    enum En{One, Second};
    class c{
    }@
    I could write En::One instead of с::One, but if i write
    @
    class c{
    enum En{One, Second};
    }@
    Compiler get me many errors
    Advance many thanks for your help!

    Edit: Moved to C++ Gurus as this is not Qt specific [ZapB]



  • Can you paste the errors you are getting at compile time.. this should work as I see it...



  • enum En is private for class c. Where do you try to access values? Member functions or outside class?



  • If it's written exactly as you showed it to us, the enum is private, meaning that you won't be able to do c::One outside of your class.

    If the enum is public, then post the real piece of code and the compilation errors as already asked



  • This works for me:

    @class MyClass
    {
    public:
    enum MyEnum
    {
    Type_One,
    Type_Two
    };
    };@



  • [quote author="KA51O" date="1311579884"]This works for me:

    @class MyClass
    {
    public:
    enum MyEnum
    {
    Type_One,
    Type_Two
    };
    };@[/quote]

    Even the code posted by Ruzik should work provided that its used only inside the class..



  • Many thanks for your help, i found my mistake



  • Just for the records: enums in C++ do not use the enum name for accessing enum values.

    @
    class TestClass
    {
    public:
    enum Enum
    {
    Value1,
    Value2,
    Value3
    };
    };
    ...
    TestClass::Enum enumVariable = TestClass::Enum::Value1; // not correct
    TestClass::Enum enumVariable = TestClass::Value1; // correct
    ...
    @

    This is why good class design usually requires that the enum name is part of the enum values (as also seen in Qt).

    @
    class TestClass
    {
    public:
    enum Error
    {
    InternalError,
    ExternalError
    };
    };
    @

    And keep in mind that class members - as already stated by others - are private by default.



  • [quote author="Lukas Geyer" date="1311582311"]Just for the records: enums in C++ do not use the enum name for accessing enum values.
    (...)
    This is why good class design usually requires that the enum name is part of the enum values (as also seen in Qt).
    [/quote]

    Also note that this is about to "change":http://en.wikipedia.org/wiki/C++0x#Strongly_typed_enumerations in C++0x. There, the enum name does become part of the value name. You could change your second example to this then:

    @
    class TestClass
    {
    public:
    class enum Error //note the 'class' in front of the enum keyword
    {
    Internal, //Note that you no longer need the Error postfix
    External
    };
    };

    ...

    TestClass::Error errorVariable = TestClass::Error::Internal; // correct in C++0x
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.