By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

The extern "C" construct - does it allow the use of C-style (int-based) enums?

P: n/a
Hello,

I am trying to include a header file which was written in C, but my
project is in C++. Even when I wrap the include statement or the
entire header files with extern "C" (used according to the
instructions on c++-faq-lite), I get the same error, which is this:

invalid conversion from `int' to `DmtxDirection'

where DmtxDirection is an enum with integer values inside (obviously).

The offending function:

static DmtxDirection
TurnCorner(DmtxDirection dir, int turn)
{
DmtxDirection newDir;

newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >1) : 0x0f & (dir
<< 1);

return (newDir) ? newDir : (dir ^ 0x0f) & 0x09;
}

And the enum:

typedef enum {
DmtxDirNone = 0x00,
DmtxDirUp = 0x01 << 0,
DmtxDirLeft = 0x01 << 1,
DmtxDirDown = 0x01 << 2,
DmtxDirRight = 0x01 << 3,
DmtxDirHorizontal = DmtxDirLeft | DmtxDirRight,
DmtxDirVertical = DmtxDirUp | DmtxDirDown,
DmtxDirRightUp = DmtxDirRight | DmtxDirUp,
DmtxDirLeftDown = DmtxDirLeft | DmtxDirDown
} DmtxDirection;
Long story short - should the extern "C" construct allow the library
to use enums in this manner, or do I need to take a different course?

Thanks for any advice.

Chris

Jun 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chris Cahoon wrote:
Hello,
<snip>
Long story short - should the extern "C" construct allow the library
to use enums in this manner, or do I need to take a different course?
It does not. extern "C" doesn't alter the behaivour of not allowing implicit
conversion from int to enum. However, you can use static_cast for that,
static_cast<DmtxDirection>(value).

--
Dizzy

Jun 12 '07 #2

P: n/a
Chris Cahoon wrote:
Hello,

I am trying to include a header file which was written in C, but my
project is in C++. Even when I wrap the include statement or the
entire header files with extern "C" (used according to the
instructions on c++-faq-lite), I get the same error, which is this:

invalid conversion from `int' to `DmtxDirection'

where DmtxDirection is an enum with integer values inside (obviously).
extern "C" changes "linkage" of functions and variables (i.e., it
affects the symbol names in the object files). It doesn't affect enums
in any way. You might want to add a type cast from int to enum to your code:
>
The offending function:

static DmtxDirection
TurnCorner(DmtxDirection dir, int turn)
{
DmtxDirection newDir;

newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >1) : 0x0f & (dir
<< 1);
newDir = static_cast <DmtxDirection((turn = DMTX_TURN_CW ...
D.
Jun 12 '07 #3

P: n/a
On Jun 12, 6:14 pm, Davlet Panech <dp1978x_li...@yahoo.cawrote:
Chris Cahoon wrote:
I am trying to include a header file which was written in C, but my
project is in C++. Even when I wrap the include statement or the
entire header files with extern "C" (used according to the
instructions on c++-faq-lite), I get the same error, which is this:
invalid conversion from `int' to `DmtxDirection'
where DmtxDirection is an enum with integer values inside (obviously).
extern "C" changes "linkage" of functions and variables (i.e., it
affects the symbol names in the object files). It doesn't affect enums
in any way. You might want to add a type cast from int to enum to your code:
The offending function:
static DmtxDirection
TurnCorner(DmtxDirection dir, int turn)
{
DmtxDirection newDir;
newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >1) : 0x0f & (dir
<< 1);
newDir = static_cast <DmtxDirection((turn = DMTX_TURN_CW ...
Which will cause a few problems when the header is compiled as
C:-). If he has access to the header, and can modify it, then
he can use a C style cast here. If he doesn't, he'll need to
"pre-process" the header somehow, and include the pre-processed
version. (If he's using make, it's almost trivial to get the
system to invoke the preprocessing automatically. If he's using
an IDE, I don't know.)

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.