July 25, 2011

Ruzik Ruzik
Lab Rat
293 posts

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:

  1. ClassName::EnumElement

If i declare enum outside class, for example
  1. enum En{One, Second};
  2. class c{
  3. }

I could write En::One instead of с::One, but if i write
  1. class c{
  2. enum En{One, Second};
  3. }

Compiler get me many errors
Advance many thanks for your help!

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

8 replies

July 25, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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

July 25, 2011

task_struct task_struct
Ant Farmer
345 posts

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

 Signature 

“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”
- Linus Torvalds

July 25, 2011

Johan Solo Johan Solo
Lab Rat
94 posts

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

 Signature 

Linux : you can find worse, but it’s more expensive.

July 25, 2011

KA51O KA51O
Robot Herder
397 posts

This works for me:

  1. class MyClass
  2. {
  3. public:
  4.   enum MyEnum
  5.   {
  6.      Type_One,
  7.      Type_Two
  8.   };
  9. };

July 25, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

KA51O wrote:
This works for me:

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

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

July 25, 2011

Ruzik Ruzik
Lab Rat
293 posts

Many thanks for your help, i found my mistake

July 25, 2011

Lukas Geyer Lukas Geyer
Dinosaur Breeder
2074 posts

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

  1. class TestClass
  2. {
  3. public:
  4.     enum Enum
  5.     {
  6.         Value1,
  7.         Value2,
  8.         Value3
  9.     };
  10. };
  11. ...
  12. TestClass::Enum enumVariable = TestClass::Enum::Value1; // _not_ correct
  13. TestClass::Enum enumVariable = TestClass::Value1;           // correct
  14. ...

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

  1. class TestClass
  2. {
  3. public:
  4.     enum Error
  5.     {
  6.         InternalError,
  7.         ExternalError
  8.     };
  9. };

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

July 25, 2011

Andre Andre
Area 51 Engineer
6075 posts
Lukas Geyer wrote:
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).

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

  1. class TestClass
  2. {
  3. public:
  4.     class enum Error //note the 'class' in front of the enum keyword
  5.     {
  6.         Internal, //Note that you no longer need the Error postfix
  7.         External
  8.     };
  9. };
  10.  
  11. ...
  12.        
  13. TestClass::Error errorVariable = TestClass::Error::Internal; // correct in C++0x

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ Are "Stacked Widgets" and "Building GUI’s using inheritance" mutually exclusive?      About default constructor ››

You must log in to post a reply. Not a member yet? Register here!