枚举变量和extern [英] enum variables and extern

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

问题描述

我有一个文件data.c,我定义了所有的全局变量。


然后我有一个头文件data.h,我将其包含在每个文件中在

中,我引用了data.c中定义的所有变量。


在大多数情况下,我对此没有任何问题。但是,我在$ b中定义了一个myenum类型的变量:


enum myenum

{

周一,

周二,

周三

} enum_var;


现在当我尝试在data.h中引用enum_var,编译器给我

问题。我已经尝试了多种方式来引用它并定义它,

但是没有任何工作。我想我只是不明白

关于enum类型和/或extern。


任何帮助都会很棒。


谢谢,


查理

I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don''t understand something
about enum types and/or extern.

Any help would be great.

Thanks,

Charlie

推荐答案

Charlie写道:
Charlie wrote:

我有一个文件data.c,我定义了所有的全局变量。


然后我有一个标题文件,data.h,我包含在

中的每个文件中,我引用了data.c中定义的所有变量。


大部分,我对此没有任何问题。但是,我在$ b中定义了一个myenum类型的变量:


enum myenum

{

周一,

周二,

周三

} enum_var;


现在当我尝试在data.h中引用enum_var,编译器给我

问题。我已经尝试了多种方式来引用它并定义它,

但是没有任何工作。我想我只是不明白

关于枚举类型和/或extern。
I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don''t understand something
about enum types and/or extern.



您需要在引用它的任何地方定义''enum myenum''。你只需要声明它就可以使用枚举,就像变量一样。所以

必须在data.h文件中。


data.c:

#include" data。 h"

enum myenum enum_var;


data.h:

extern enum myenum

{ ...} enum_var;





data.h:

enum myenum

{...};

extern enum myenum enum_var;

You need to define ''enum myenum'' everywhere you reference it. You
can''t use an enum by merely declaring it, as you can for variables. So
it has to be in the data.h file.

data.c:
#include "data.h"
enum myenum enum_var;

data.h:
extern enum myenum
{ ... } enum_var;

or

data.h:
enum myenum
{ ... };
extern enum myenum enum_var;


Charlie写道:
Charlie wrote:

我有一个文件data.c,我定义了所有的全局变量。


然后我有一个头文件data.h ,我在每个文件中包含了

,我引用了data.c中定义的所有变量。


在大多数情况下,我没有这个问题。但是,我在$ b中定义了一个myenum类型的变量:


enum myenum

{

周一,

周二,

周三

} enum_var;


现在当我尝试在data.h中引用enum_var,编译器给我

问题。我已经尝试了多种方式来引用它并定义它,

但是没有任何工作。我想我只是不明白

关于枚举类型和/或extern。
I have a file, data.c, where I define all of my global variables.

I then have a header file, data.h, which I include in every file in
which I reference all of the variables defined in data.c.

For the most part, I am having no problem with this. However, I
defined an variable of type myenum in data.c:

enum myenum
{
Mon,
Tues,
Wed
} enum_var;

Now when I try to reference enum_var in data.h, the compiler gives me
issues. I have tried multiple ways of referencing it and defining it,
but nothing is working. I guess I just don''t understand something
about enum types and/or extern.




/ * ah * /

enum myenum {周一,周二,周三};

enum myenum fun(enum myenum x);

/ * ac * /

#include< stdio.h>

#include" ; a.h"


int main(无效)

{

enum myenum y =周一;

printf(用y =%d \ n调用乐趣,y);

y = fun(y);

printf(" Now y =%d \ n",y);

返回0;

}

/ * bc * /

#include" a.h"

enum myenum fun(enum myenum x)

{

返回x + 1;

}



/* a.h */
enum myenum { Mon, Tues, Wed };
enum myenum fun(enum myenum x);
/* a.c */
#include <stdio.h>
#include "a.h"

int main(void)
{
enum myenum y = Mon;
printf("Calling fun with y = %d\n", y);
y = fun(y);
printf("Now y = %d\n", y);
return 0;
}
/* b.c */
#include "a.h"

enum myenum fun(enum myenum x)
{
return x + 1;
}


Charlie在04/30/07 10:35写道:
Charlie wrote On 04/30/07 10:35,:

我有一个文件data.c,我定义了所有的全局变量。

[...]
I have a file, data.c, where I define all of my global variables.
[...]



也许你应该解释为什么你不满意

以及你在发布同样问题时得到的答案
$ b一周前$ b。否则,很有可能你会得到同样的答案

再次回答并且没有比你现在更好的了。

现在。


-
Er ********* @ sun.com


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

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