简体   繁体   中英

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.

#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)

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 . So, to avoid the redefinition of BOOL , do following when you are providing definition to bool :

typedef enum BOOL bool;

So you have followings:

File1.h

#ifndef _TABLE_H
#define _TABLE_H

typedef enum BOOL { false, true } Boolean;

#endif //_TABLE_H

File2.h

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

Here, you are providing definition for BOOL in File1.h . In File2.h you are just providing an alias name.
Note: It would be better if you just provide deceleration in File1.h and put definition of BOOL in some another file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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