I need to use some C code in a C++ project but I'm getting build errors for one of the c original's .h files.
The error has to do with some typedef enum parts, which I undestand that c and c++ handles differently. (or c++ doesn't do)
Here is a minimal version of that c style .h:
(there's several more similar entries in the real file)
typedef enum {
ILCLIENT_EMPTY_BUFFER_DONE = 0x1 /**< Set when a buffer is
returned from an input
port */
} ILEVENT_MASK_T;
The error message I get is:
pihwdecode.cpp:35:99: error: invalid conversion from ‘int’ to ‘ILCLIENT_CREATE_FLAGS_T’ [-fpermissive]
if(ilclient_create_component(client, &video_decode, "video_decode", ILCLIENT_DISABLE_ALL_PORTS | ILCLIENT_ENABLE_INPUT_BUFFERS) != 0)
I can't start rewriting and rebuilding all the C libraries so how can I add this to the c++ project? I have tried to wrap the #include call in a extern "C" block but that doesn't help.
All suggestions welcome! Cheers, Fred
One trick you can do is defining an overloaded operator |
for enum types that are intended to be used as bitmasks like this:
ILCLIENT_CREATE_FLAGS_T operator|(ILCLIENT_CREATE_FLAGS_T a, ILCLIENT_CREATE_FLAGS_T b) {
return static_cast<ILCLIENT_CREATE_FLAGS_T>(static_cast<int>(a) | static_cast<int>(b)); }
This will then allow you to |
together mulitple such flags without additional casts in your C++ code.
The reason this is useful for allowing more C-like code -- in C you can implicitly convert an enum to an int or an int to an enum. In C++ you can do the first, but not the second.