联盟嵌套在结构中。 [英] Union nested in structure.

查看:64
本文介绍了联盟嵌套在结构中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个C文件,说struct.c具有以下结构

声明在同一个文件中


struct A

{

联盟钥匙

{

int i;

浮动f;

} k1;

};

struct B

{

联盟钥匙

{

int i;

浮动f;

} k2;

};

当我使用

gcc -c struct.c编译此代码时


我收到了一个错误


重新定义'union key''在联合密钥的定义中在

struct B.

这不是范围联盟密钥吗?结构A中定义的限制为

struct A?从行为来看,嵌套的工会看起来像是全球范围。


有人能告诉我这种行为的原因吗?

当我把文件作为struct.cpp编译时,它编译没有任何问题。

类似的行为,如果我在结构中嵌入一个结构...

I created a C file say struct.c with the following structure
declarations in the same file

struct A
{
union key
{
int i;
float f;
}k1;
};
struct B
{
union key
{
int i;
float f;
}k2;
};
When I compiled this code using
gcc -c struct.c

I got a error

"redefinition of `union key''" at the definition of "union key" in
struct B.
Is it not that the scope "union key" defined in struct A is limited to
struct A? From the behaviour it looks like that nested unions have
global scope.

Can anyone provide me the reason for such behavior?

When i made the file as struct.cpp it compiled without any problem.
Similar is the behaviour if I nest a struct inside a struct..

推荐答案


ta*****@gmail.com 写道:
我创建了一个C文件,说struct.c,具有以下结构
声明在同一个文件中

struct A
{
工会密钥
{
int i;
浮动f;
} k1;
};

struct B
{
联合键
{i /
浮动f;
} k2;
};

当我使用
gcc -c struct.c编译这段代码时我得到了一个错误

重新定义`union key''"在联合密钥的定义中在
结构B.

这不是范围联合密钥吗?结构A中定义的结构仅限于结构A?从行为来看,嵌套联盟看起来像全局范围。

任何人都可以告诉我这种行为的原因吗?

当我把文件作为结构.cpp编译没有任何问题。


恭喜:您刚刚发现两种语言C和C ++之间存在差异。

C有_one_名称空间for _all_ structure,union和enumeration tags。

无范围。实际上,你可以这样写:

struct A

{

联盟钥匙

{

int i;

浮动f;

} k1;

};


struct B

{

联盟钥匙k2;

};



联盟key

{

int i;

浮动f;

};


struct A

{

联盟密钥k1;

};


struct B

{

联盟钥匙k2;

};

干杯

迈克尔


类似的行为如果我在结构中嵌入一个结构...
I created a C file say struct.c with the following structure
declarations in the same file

struct A
{
union key
{
int i;
float f;
}k1;
};
struct B
{
union key
{
int i;
float f;
}k2;
};
When I compiled this code using
gcc -c struct.c

I got a error

"redefinition of `union key''" at the definition of "union key" in
struct B.
Is it not that the scope "union key" defined in struct A is limited to
struct A? From the behaviour it looks like that nested unions have
global scope.

Can anyone provide me the reason for such behavior?

When i made the file as struct.cpp it compiled without any problem.
Congratulations: You have just found one of the differences between
the two languages C and C++.
C has _one_ namespace for _all_ structure, union and enumeration tags.
No scope. Effectively, you can write:
struct A
{
union key
{
int i;
float f;
}k1;
};

struct B
{
union key k2;
};
or
union key
{
int i;
float f;
};

struct A
{
union key k1;
};

struct B
{
union key k2;
};
Cheers
Michael

Similar is the behaviour if I nest a struct inside a struct..




正如语言标准所要求的那样。

干杯

Michael

-

电子邮件:我的是gmx dot de address。 />



Exactly as the language standard demands it.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.


但为什么C标准要求这样的行为呢?关于范围,C非常特别

。为什么

结构,联合和枚举不遵循范围规则?

But why does C standard demand such a behaviour? C is very particular
about scope. Why is that scope rules are not followed in case of
structures, unions and enums?


Tapeesh写道:
Tapeesh wrote:

但是为什么C标准要求这样的行为呢?关于范围,C非常特别。为什么在结构,联合和枚举的情况下不遵循范围规则?

But why does C standard demand such a behaviour? C is very particular
about scope. Why is that scope rules are not followed in case of
structures, unions and enums?




你的问题毫无意义。什么行为?在usenet所谓的

之前的文章通常在接收站没有,

所以每篇文章都应该独立。这就是为什么我们包括

归因和报价。即使谷歌打破了测试系统

你也可以这样做,请看下面我的签名。


-

"如果你想通过groups.google.com发布跟进,不要使用

破解的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson



Your question makes no sense. What behaviour? In usenet so called
previous articles are often not available at the receivers station,
so each article should stand on its own. That is why we include
attributions and quotations. Even with googles broken beta system
you can do it, see my sig below.

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


这篇关于联盟嵌套在结构中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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