访问结构体中定义的枚举的值
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
枚举值成为类范围内的名称。因此,您可以从类外部使用
padData::select
,或者仅从类内部使用select
。在 C++11 中,您可以使用枚举名称来限定枚举器,从外部给出
padData::buttonsAndAxes::select
,从内部给出buttonsAndAxes::select
。在 C++ 中打印枚举器的名称并不容易,因为名称在编译后就消失了。您需要手动设置一个将值映射到字符串的表。如果您没有像示例中那样提供显式值,则可以简单地使用数组:
然后索引到该数组:
如果您想要一些更复杂的方法,您可以在 上一页 问题。
The enum values become names in the scope of the class. So you would use
padData::select
from outside the class, or justselect
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 andbuttonsAndAxes::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:
And then you index into that array:
If you want some more sophisticated approach, you can find a bunch of tricks in previous questions.
枚举不是数组,它在没有索引的情况下使用。
Enum is not array, it is used without index.
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
你似乎需要一本好的 C++ 书。
在 C 和 C++ 中,枚举是一种便捷的方法:
语法非常简单(在 C++03 中):
其中:
是引入的类型的名称
是枚举值的名称
是为名称指定的值,并且是可选的如果没有为名称指定值:
0
+ 1
这是一个演示枚举使用的小示例:
这同时说明了几个重要点:
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:
The syntax is quite simple (in C++03):
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 optionalIf no value is given to a name:
0
+ 1
Here is a small example demonstrating the use of enums:
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.enum
"value-name" is an integral constant, it can be used as template parameter or in switch cases.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 theenum class
syntax)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 plainint
.Therefore:
printf("%d", Green);
is a programming error. It should beprintf("%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.