Reputation: 10144
I have a C project with a structure as follows:
Project
- fileA.h
- fileA.c
+ subdirB
- fileD.h
There is a typedef enum of bool in fileD.h:
typedef enum bool {
false = 0, /**< false (0) */
true /**< true (1) */
} bool;
In fileA.c there are several functions that use bool as parameters and so fileD.h is included:
#include "subdirB/fileD.h" // header with bool typedef
#include "fileA.h" // own header with foo declaration
...
int foo (bool bar) {
...
return 0;
}
...
These functions are to be made global so their declaration is added to the header fileA.h:
int foo (bool bar);
This last bit however gives me an error that "the identifier bool is undefined"... I dont understand why this would be the case though??
Upvotes: 0
Views: 142
Reputation: 70392
As noted earlier, fileA.h
needs to be aware of the bool
type defined in fileD.h
so that the function prototype for foo()
is valid.
However, note that since your bool
is not a built-in type, but an enum
, there is no implicit conversion to true
for all non-zero values. So do not count on code within foo()
that looks like:
if (bar == true) {
//...
}
to work in the general case. You can alleviate this somewhat with a macro replacement for foo()
:
#define foo(x) foo(!!(x))
This becomes tedious if you have many functions that take a bool
. But the !!
trick itself works to turn non-zero values into 1
for you though.
Upvotes: 1
Reputation: 194
Are you including fileD.h
in fileA.h
? In order to declare a function that takes a bool, the compiler needs to know what a bool is.
Upvotes: 4