2

The struct is as follows:

struct padData 
{
    enum buttonsAndAxes
    {
        select,
        start,
        ps
    };
};

The object of the struct:

padData        pad;

I am accessing this enum as follows:

printf ("\n%d", pad.buttonsAndAxes[0]);

Error:

error: invalid use of ‘enum padData::buttonsAndAxes’

Then, I tried:

printf ("\n%d", pad::buttonsAndAxes[0]);

Error:

error: ‘pad’ is not a class or namespace

Now what? Please guide.

Compiler: gcc version 4.5.0

EDIT 1:____________________________________

printf ("\nemit: %d", padData::(select)0);

results in:

error: expected unqualified-id before ‘(’ token

My aim is to fetch the word "select" through its value 0. How to achieve that? Also, is the word "select" a string?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • You want that to print "0" or "select"? – R. Martinho Fernandes Dec 01 '11 at 08:55
  • @R.MartinhoFernandes thanks for responding, I want to supply 0 to that statement and get the result "select". i.e. I want to print select. – Aquarius_Girl Dec 01 '11 at 08:56
  • 1
    Oh, that needs to be done by hand :( That is, you need to make a table with the values and the corresponding strings. Here's a similar question: http://stackoverflow.com/questions/3342726/c-print-out-enum-value-as-text – R. Martinho Fernandes Dec 01 '11 at 08:57
  • @R.MartinhoFernandes a small example please. :) – Aquarius_Girl Dec 01 '11 at 08:58
  • @R.MartinhoFernandes See this answer, how to make it work w.r.t the enum in a struct: http://stackoverflow.com/a/321828/462608 – Aquarius_Girl Dec 01 '11 at 09:00
  • alright @R.MartinhoFernandes That seems to be a horrific task! :( Thanks anyway. – Aquarius_Girl Dec 01 '11 at 09:02
  • Yes, it's mighty ugly :( I provided a simple example with no tricks in my answer. If you want something "sophisticated" (and ugly) you should browse that question and similar ones. – R. Martinho Fernandes Dec 01 '11 at 09:06
  • @R.MartinhoFernandes Thanks for the example, but in that case does it make any sense to use an enum? Isn't it better to create a char* array and access its indexes? Just asking. – Aquarius_Girl Dec 01 '11 at 09:09
  • 1
    the enum lets you also use the name in your code. It makes it more readable. That's the whole point of using enums. But yes, you could do without it. – R. Martinho Fernandes Dec 01 '11 at 09:10
  • @R.MartinhoFernandes Thanks for answering, but IMO the aim should be to make the life easy and that enum is trying its best to make things difficult for me. I somehow don't think it is worth the effort. You were helpful, thanks. – Aquarius_Girl Dec 01 '11 at 09:13
  • @R.MartinhoFernandes Also, please add that link in your answer. It might help someone else too. :) – Aquarius_Girl Dec 01 '11 at 09:15

4 Answers4

3

The enum values become names in the scope of the class. So you would use padData::select from outside the class, or just select from inside the class.

In C++11 you can qualify the enumerators with the name of the enum, giving padData::buttonsAndAxes::select from the outside and buttonsAndAxes::select from inside.


Printing the name of an enumerator is not easily done in C++, because the names are gone after compilation. You need to set up a table mapping the values to their strings by hand. If you don't supply explicit values like in your example, you can simply use an array:

enum buttonsAndAxes
{
    select,
    start,
    ps
};

const char* buttonsAndAxesNames[] = {
    "select",
    "start",
    "ps"
};

And then you index into that array:

 printf("%s", buttonsAndAxesNames[select]);

If you want some more sophisticated approach, you can find a bunch of tricks in previous questions.

Community
  • 1
  • 1
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
2
printf ("\n%d", padData::select);

Enum is not array, it is used without index.

Alex F
  • 42,307
  • 41
  • 144
  • 212
1

ENUMS are mainly used for better readability of code rather than calculation facilitators. ENUMS are mainly literals which are assigned values 0,1,2 etc unless specified otherwise. So you should always use them with "::" qualification rather than as array

1

You seem to need a good C++ book.

Enumerations, in C and C++, are a convenient way to:

  • map an integral value to a "smart" name
  • group together values that belong together

The syntax is quite simple (in C++03):

enum <enum-name> {
  <value-name-0> [= <value-0>],
  <value-name-1> [= <value-1>],
  ...
};

Where:

  • <enum-name> is the name of the type that is introduced
  • <value-name-X> is the name of a value of the enum
  • <value-X> is the value given to the name, and is optional

If no value is given to a name:

  • if it is the first, it is set to 0
  • else, it is set to the value of the previous name, + 1

Here is a small example demonstrating the use of enums:

enum Color {
  Blue,
  Green,
  Red
};

char const* name(Color c) {
  switch(c) {
  case Blue: return "Blue";
  case Green: return "Green";
  case Red: return "Red";
  }
  assert(0 && "Who stored crap in my enum ?");
}

This illustrates a few important points at once:

  • Color is a type, like a struct type or a class type. It can be typedefed and all.
  • an enum "value-name" is an integral constant, it can be used as template parameter or in switch cases.
  • an enum "value-name" is injected in the scope in which the type is declared, and not nested within. (C++11 allows to scope the values with the enum class syntax)
  • something else entirely could be stored in the enum, while this should not happen in well behaved applications, you can do it through casting...

What is not shown, is that an enum is under the hood a plain integer. The exact underlying type though is determined at the discretion of the compiler. There are a few rules in this choice, that should not matter to you, all you should know is that the type chosen is wide enough to contain all the values of the enum (and possibly signed if required). What it implies is that the type chosen is not necessarily a plain int.

Therefore: printf("%d", Green); is a programming error. It should be printf("%d", (int)Green);.

Another important point, is that enum names do not appear in the final binary. The names are substituted for their values directly, no runtime overhead at all. Debuggers typically retrieve the names from the debug information (if available) and substitute them back in when presenting the information to you.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722