473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
3 2265
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
3
by: j0mbolar | last post by:
void myfunction(void) { extern int myvariable; return ; } what is the point in allowing local variables to have external linkage? its scope is only myfunction so it can't be used anywhere...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
5
by: profdotnet | last post by:
Below is the code of web.config file: <configuration> <system.web> <authentication mode="Forms" /> <authorization> <allow users="Admin"/> <deny users="Jack,Mary" /> <deny users="?">...
21
by: raylopez99 | last post by:
Well, contrary to the implication in my 2000 textbook on C# (public beta version), C# does allow multiple inheritance, so long as it's serially chained as follows: class derived02 : derived01 {...
4
by: zdravko.monov | last post by:
Hi! I am trying to get several parameters in functions as enum variables. Consider this: namespace Layout { enum type { DOT, NEATO, FDP, TWOPI, CIRCO };
0
by: vk02720 | last post by:
"Art S. Kagel" <kagel@bloomberg.netwrote in message news:<pan.2003.07.17.18.01.18.408954.15473@bloomberg.net>... Thanks. So what level of SQL standard does Informix support ? And again, is this...
2
by: John Kotuby | last post by:
Hi all, I am integrating some 3rd party grids into a large ASPX form page in VS 2008 using VB. I have 4 different grids on the page. The object is to not allow the user to leave a grid if...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.