枚举类型和外部 [英] enum types and extern

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

问题描述

我之前尝试过这个帖子,如果我重复自己,我会道歉,

但是我在任何地方都看不到帖子。


但无论如何,


我有一个文件data.c,我定义了所有的全局变量。然后我使用extern关键字从标题中引用这些变量

文件data.h,我将其包含在每个文件中。


我在data.c中定义一个枚举类型和变量:


enum myenum

{

val1,

val2,

val3,

} enum_var;


当我尝试引用enum_var时data.h我总是收到错误。我已经尝试了很多方法来引用它并定义它但是我好像b $ b总是会出现编译错误。


所以我如何从外部引用data.h中的enum_var,以便在程序中使用

enum_var?如果我完全忽略了

这一点,如果有人能够清除我对enum或extern的混淆那么

会很棒。

谢谢,


查理

I tried to post this before and I apologize if I am repeating myself,
but I do not see the post anywhere.

But anyway,

I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:

enum myenum
{
val1,
val2,
val3,
} enum_var;

When I try to reference enum_var in data.h I always get an error. I
have tried numerous ways of referencing it and defining it but I
always seem to get a compiler error.

So how can I externally reference enum_var in data.h so that I can use
enum_var in my program? And if I am totally missing the point on
this, if someone could clear up my confusion about enum or extern that
would be great.

Thank you,

Charlie

推荐答案

2007年4月24日08:19:40 -0700,在comp.lang.c,Charlie

< ev ********** @ yahoo.comwrote:

On 24 Apr 2007 08:19:40 -0700, in comp.lang.c , Charlie
<ev**********@yahoo.comwrote:


>我有一个文件data.c,我定义了所有的全局变量。然后我使用extern关键字从标题
文件data.h中引用这些变量,我将其包含在每个文件中。

我定义了一个枚举类型和变量在data.c中:
>I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:



< snip>

<snip>


>当我尝试在data.h中引用enum_var时,我总是收到错误。
>When I try to reference enum_var in data.h I always get an error.



将枚举的定义移动到标题中,将变量放在

的源文件中,并将标题中的extern引用放在下面enum

定义。


顺便说一句,全局变量在C中通常被认为是个坏主意。


-

马克麦金太尔


调试的难度是首先编写代码的两倍。

因此,如果您尽可能巧妙地编写代码,那么,根据定义,它不够聪明,无法调试它。

--Brian Kernighan

Move the definition of the enum to your header, put the variable in
the source file and the extern reference in the header, below the enum
definition.

By the way, globals are generally considered a bad idea in C.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan


Mark McIntyre写道:
Mark McIntyre wrote:

>
>



.... snip ...

.... snip ...


>

顺便说一句,全局变量在C中通常被认为是个坏主意。
>
By the way, globals are generally considered a bad idea in C.



取决于应用程序。


-

< http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

< http://www.securityfocus。 com /专栏作家/ 423>

< http://www.aaxnet.com/editor/edit043.html>

cbfalconer at maineline.net


-

通过的免费Usenet帐户发布http://www.teranews.com

Depends on the application.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net

--
Posted via a free Usenet account from http://www.teranews.com


CBFalconer写道:
CBFalconer wrote:

Mark McIntyre写道:
Mark McIntyre wrote:

>>
>>



... snip ...

... snip ...


>>
顺便说一下,全局变量在C中通常被认为是个坏主意。
>>
By the way, globals are generally considered a bad idea in C.



取决于应用程序。


Depends on the application.



大多数自然数大于17,尽管存在

5,11和15.

-

如果你不教我,我怎么理解? - Trippa,/ Falling /


Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN

Most natural numbers are greater than 17, despite the existence of
5, 11, and 15.

--
"How am I to understand if you won''t teach me?" - Trippa, /Falling/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


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

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