简体   繁体   English

避免在C中重新定义枚举

[英]Avoiding redefinition of enums in C

I need an explanation as to how to make this work (if it's even possible). 我需要有关如何进行这项工作的说明(如果可能的话)。

So, in my header file, I have an enum declaration for a BOOL type with some routines I am supposed to implement. 因此,在我的头文件中,我有一个BOOL类型的枚举声明以及应该实现的一些例程。

#ifndef _TABLE_H
#define _TABLE_H

typedef enum BOOL { false, true } Boolean;

#endif //_TABLE_H

I'm using this interface in a file that has a definition of various routines using the bool type (not Boolean this time) 我在文件中使用此接口,该文件使用bool类型定义了各种例程(这次不是布尔值)

typedef enum BOOL { false, true } bool;

I want to be able to use both, how do I do this? 我希望能够同时使用两者,我该怎么做?

In your provided code you are redefining BOOL . 在提供的代码中,您正在重新定义BOOL So, to avoid the redefinition of BOOL , do following when you are providing definition to bool : 因此,为避免重新定义BOOL ,请在为bool提供定义时执行以下操作:

typedef enum BOOL bool;

So you have followings: 因此,您有以下内容:

File1.h 文件1.h

#ifndef _TABLE_H
#define _TABLE_H

typedef enum BOOL { false, true } Boolean;

#endif //_TABLE_H

File2.h 文件2.h

#include <File1.h>
...
typedef enum BOOL bool;
...

Here, you are providing definition for BOOL in File1.h . 在这里,您将在File1.h中提供BOOL定义。 In File2.h you are just providing an alias name. File2.h您仅提供别名。
Note: It would be better if you just provide deceleration in File1.h and put definition of BOOL in some another file. 注意:如果只在File1.h提供减速度,然后将BOOL定义放在另一个文件中,那会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM