访问结构体中定义的枚举的值

发布于 2024-12-19 02:49:07 字数 1104 浏览 3 评论 0原文

struct 如下:

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

struct 的对象:

padData        pad;

我按如下方式访问此枚举:

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

错误:

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

然后,我尝试了:

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

错误:

error: ‘pad’ is not a class or namespace

现在怎么办?请指导。

编译器:gcc 版本 4.5.0

编辑1:_________ ________________ ___________

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

结果in:

error: expected unqualified-id before ‘(’ token

我的目标是通过值 0 获取单词“select”。如何实现这一点?另外,“select”这个词是一个字符串吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩心态 2024-12-26 02:49:07

枚举值成为类范围内的名称。因此,您可以从类外部使用 padData::select ,或者仅从类内部使用 select

在 C++11 中,您可以使用枚举名称来限定枚举器,从外部给出 padData::buttonsAndAxes::select,从内部给出 buttonsAndAxes::select


在 C++ 中打印枚举器的名称并不容易,因为名称在编译后就消失了。您需要手动设置一个将值映射到字符串的表。如果您没有像示例中那样提供显式值,则可以简单地使用数组:

enum buttonsAndAxes
{
    select,
    start,
    ps
};

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

然后索引到该数组:

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

如果您想要一些更复杂的方法,您可以在 上一页 问题

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.

盛装女皇 2024-12-26 02:49:07
printf ("\n%d", padData::select);

枚举不是数组,它在没有索引的情况下使用。

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

Enum is not array, it is used without index.

飞烟轻若梦 2024-12-26 02:49:07

ENUM 主要用于提高代码的可读性,而不是促进计算。 ENUMS 主要是文字,除非另有说明,否则它们被分配值 0、1、2 等。因此,您应该始终将它们与“::”限定一起使用,而不是作为数组

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

蓝天白云 2024-12-26 02:49:07

你似乎需要一本好的 C++ 书。

在 C 和 C++ 中,枚举是一种便捷的方法:

  • 将整数值映射到“智能”名称
  • 将属于同一组的值组合在一起

语法非常简单(在 C++03 中):

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

其中:

  • 是引入的类型的名称
  • 是枚举值的名称
  • 是为名称指定的值,并且是可选的

如果没有为名称指定值:

  • 如果是第一个,则设置为 0
  • 否则,它被设置为先前名称的值,+ 1

这是一个演示枚举使用的小示例:

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 ?");
}

这同时说明了几个重要点:

  • Color > 是一个类型,就像结构类型一样或类类型。它可以是类型定义的等等。
  • enum“value-name”是一个整型常量,它可以用作模板参数或在 switch 情况下使用。
  • enum“值名称”被注入到声明类型的范围中,而不是嵌套在其中。 (C++11 允许使用 enum class 语法来确定值的范围)
  • 完全可以在 enum 中存储其他内容,但在行为良好的应用程序中不应发生这种情况,你可以通过转换来做到这一点...

没有显示的是,enum在幕后是一个普通的整数。但确切的基础类型是由编译器自行决定的。此选择中有一些规则,这对您来说不重要,您应该知道的是选择的类型足够宽以包含枚举的所有值(如果需要,还可以签名)。它意味着选择的类型不一定是普通的int

因此: printf("%d", Green); 是一个编程错误。它应该是printf("%d", (int)Green);

另一个重要的一点是,枚举名称不会出现在最终的二进制文件中。名称直接替换它们的值,根本没有运行时开销。调试器通常从调试信息(如果可用)中检索名称,并在向您呈现信息时将其替换回来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文