473,785 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Dmtx Direction 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,
DmtxDirHorizont al = 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 2286
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<Dmt xDirection>(val ue).

--
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(Dmtx Direction 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(Dmtx Direction 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 objektorientier ter Datenverarbeitu ng
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
2651
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
5313
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 else.
17
4934
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
16439
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="?"> </authorization>
21
2483
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 { } class derived01 : baseclass01
4
2499
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
940
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 construct a SQL standard ( as compared to purely an Oracle extension ) ? Any good website for SQL standard ?
2
2762
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 he/she is in the middle of editing a row and has not selected the button to "save" the changes to the client-mode grid. BTW, none of these "changes" are actually saved to the database until the entire page is posted back (I am not using Callbacks on...
0
9491
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.