C中的命名空间 [英] Name spaces in C

查看:90
本文介绍了C中的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

typedef struct Y {int X;} X;
enum E {X};

会产生错误:

错误:"X"被重新声明为另一种符号

error: 'X' redeclared as different kind of symbol

据我所知,C为结构,联合和枚举标记以及它们的成员隐式定义了名称空间.因此,我不确定为什么E::X与typedef结构标记X冲突?

As I know, C has implicitly defined namespaces for structure, union, and enum tags and also for their members. So, I'm not sure why does E::X collide with typedef structure tag X?

C中的名称空间到底是什么?

What exactly are name spaces in C?

推荐答案

C对于enum成员没有单独的命名空间.编写enum {X}时,将创建一个全局常量X(它可以与其他全局名称(例如typedef'd标签)冲突).

C does not have a separate namespace for enum members. When you write enum {X}, that creates a global constant X (which can clash with other global names such as typedef'd tags).

这篇关于C中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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