枚举类型的匿名结构 [英] enum-type anonymous structs

查看:86
本文介绍了枚举类型的匿名结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在gcc中编译它,它会说:错误:请求成员

`baz''在非结构或联合的东西中。

关于如何制作一个结构的任何变通方法或提示

表现得像一个枚举,但其成员可以用''来表达。''?

我不喜欢我想要使​​用#defines来做这件事,因为它太多了

凌乱。

代码:

static struct {

static const int baz = 1;

} bar;


void foo(){

int x = bar.baz;

}

If I try compiling this in gcc, it says: "error: request for member
`baz'' in something not a structure or union".
Any workarounds or tips on how to make a structure such that it
behaves like an enum but its members can be addressed with ''.''?
I don''t want to have to do this using #defines, as it would be far too
messy.
code:
static struct {
static const int baz = 1;
} bar;

void foo() {
int x = bar.baz;
}

推荐答案

andreyvul< andrey .... @ gmail.comwrote:
andreyvul <andrey....@gmail.comwrote:

如果我尝试在gcc中编译它,它会说:错误:请求

成员`baz''在某个结构中或者联合。

关于如何制作这样的结构的任何变通方法或提示

它表现得像枚举,但其成员可以是

用''。''解决?
If I try compiling this in gcc, it says: "error: request
for member `baz'' in something not a structure or union".
Any workarounds or tips on how to make a structure such
that it behaves like an enum but its members can be
addressed with ''.''?



No.

No.


我不想使用#defines这样做,因为它将是b / b
太乱了。

代码:

静态结构{

* * static const int baz = 1;
I don''t want to have to do this using #defines, as it
would be far too messy.
code:
static struct {
* * static const int baz = 1;



这不合法C.

This is not legal C.


} bar;


void foo(){

* * int x = bar.baz;

}
} bar;

void foo() {
* * int x = bar.baz;
}



C ++可能具有你正在寻找的功能。


明智的命名约定就像你将来的那样接近

in C.

-

Peter

C++ probably has the feature you''re looking for.

Judicious naming conventions is as close as you''ll come
in C.

--
Peter


1月2日晚上8:03,Peter Nilsson< ai。 .. @ acay.com.auwrote:
On Jan 2, 8:03 pm, Peter Nilsson <ai...@acay.com.auwrote:

andreyvul< andrey .... @ gmail.comwrote:
andreyvul <andrey....@gmail.comwrote:

如果我尝试在gcc中编译它,它会说:错误:请求

成员`baz''在非结构或联合的东西中。

关于如何制作这样一个结构的任何变通方法或技巧

它的行为就像一个枚举,但它的成员可以用''。''来代表

If I try compiling this in gcc, it says: "error: request
for member `baz'' in something not a structure or union".
Any workarounds or tips on how to make a structure such
that it behaves like an enum but its members can be
addressed with ''.''?



No.


No.


我不想使用#defines这样做,因为它将会非常混乱。

代码:

静态结构{

static const int baz = 1 ;
I don''t want to have to do this using #defines, as it
would be far too messy.
code:
static struct {
static const int baz = 1;



这不合法C.


This is not legal C.


} bar;
} bar;


void foo(){

int x = bar.baz;

}
void foo() {
int x = bar.baz;
}



C ++可能具有您正在寻找的功能。


C++ probably has the feature you''re looking for.



我知道。

I know.


明智的命名惯例尽可能接近

in C.
Judicious naming conventions is as close as you''ll come
in C.



:(


所以我坚持#defines,然后呢?

:(

So I''m stuck to #defines, then?


-

Peter
--
Peter


2008年1月2日星期三16:12:14 -0800(太平洋标准时间),andreyvul

< an ******** @ gmail.comwrote:
On Wed, 2 Jan 2008 16:12:14 -0800 (PST), andreyvul
<an********@gmail.comwrote:

>如果我尝试在gcc中编译它,它说:错误:请求成员
`baz''在某种结构或联合的东西中。
任何关于如何建立一个结构的解决方法或提示
表现得像一个枚举,但其成员可以用''。''来解决?
我不想使用#defines这样做,因为它太麻烦了。
代码:
静态结构{

static const int baz = 1;
}吧;

void foo(){

int x = bar.baz; }
>If I try compiling this in gcc, it says: "error: request for member
`baz'' in something not a structure or union".
Any workarounds or tips on how to make a structure such that it
behaves like an enum but its members can be addressed with ''.''?
I don''t want to have to do this using #defines, as it would be far too
messy.
code:
static struct {
static const int baz = 1;
} bar;

void foo() {
int x = bar.baz;
}



枚举和结构的功能是如何相交的?


-

Dan Henry

How does the functionality of enums and structures intersect?

--
Dan Henry


这篇关于枚举类型的匿名结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆