Board index » cppbuilder » two enum questions

two enum questions


2005-11-19 01:10:14 PM
cppbuilder114
1 - I have a dll that has some functions declared extern "c".
One of the functions has a code passed in which tells the
function what to do.
I declared this, but had to comment out the declaration of the first
parameter of the function AddData, and use an int instead of the
enum. I thought it would be OK to use an enum, but it did not like the
declaration. Can someone explain what I'm doing wrong there?
2 - I have three cpp files that include this header file, but the
linker is complaining about Public symbol '_TAnnotInsertOperation'
defined in both module ......
I'm sure I must be doing something stupid to get this warning, but I'm
not sure how to get rid of it besides not including the header in the
other modules.. I'd appreciate any comments/tutorials/educational
remarks.
Here is the header both questions concern:
:
#ifdef __DLL__
#define __EXPORT_TYPE __declspec(dllexport)
#else
#define __EXPORT_TYPE __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */
enum TAnnotInsertOperation {aioAddMfr, aioAddManual,
aioAddNewPageForImage, aioAddMfrPart,
aioAssocItemMfrPart, aioAddMfrHotspot, aioAddItemAnnotNC,
aioAddGotoDocAnnotNC,
aioAddGotoAssetAnnotNC, aioAddgotoLocationAnnotNC}
TAnnotInsertOperation;
long __EXPORT_TYPE InitializeWithCallBacks(HWND hWnd,//parentwindow
handle
void (*ProcessComplete)(long lId), //use to notify when
shutting down dialog
int (*AddData)( int operation,//TAnnotInsertOperation
operation, //use to add data to the system
char * manualName, char* manualDesc1, char*
manualDesc2,
char * path, char * imagefilename,
char * label,
char * item, char * mfrpartnum, char *
partdesc, char * mfrcode,
char * gotoDocument, char * gotoAssetNum, char
* gotoLocationNum ) );
#ifdef __cplusplus
}
#endif /* __cplusplus */
Thanks
 
 

Re:two enum questions

"Jeff Kish" < XXXX@XXXXX.COM >wrote in message
Quote
1 - I have a dll that has some functions declared extern "c".
One of the functions has a code passed in which tells the
function what to do.
I declared this, but had to comment out the declaration of the first
parameter of the function AddData, and use an int instead of the
enum. I thought it would be OK to use an enum, but it did not like
the
declaration. Can someone explain what I'm doing wrong there?
Did you set the compiler options to treat enums as ints? If that
doesn't help, you can probably cast the enum to an int and back
without trouble.
Quote

2 - I have three cpp files that include this header file, but the
linker is complaining about Public symbol '_TAnnotInsertOperation'
defined in both module ......
I'm sure I must be doing something stupid to get this warning, but
I'm
not sure how to get rid of it besides not including the header in
the
other modules.. I'd appreciate any comments/tutorials/educational
remarks.

enum TAnnotInsertOperation {aioAddMfr, aioAddManual,
aioAddNewPageForImage, aioAddMfrPart,
aioAssocItemMfrPart, aioAddMfrHotspot, aioAddItemAnnotNC,
aioAddGotoDocAnnotNC,
aioAddGotoAssetAnnotNC, aioAddgotoLocationAnnotNC}
TAnnotInsertOperation;
Get rid of that last TAnnotInsertOperation, i.e.
enum TAnnotInsertOperation {aioAddMfr, aioAddManual,
aioAddNewPageForImage, aioAddMfrPart,
aioAssocItemMfrPart, aioAddMfrHotspot, aioAddItemAnnotNC,
aioAddGotoDocAnnotNC,
aioAddGotoAssetAnnotNC, aioAddgotoLocationAnnotNC};
--
Bruce
 

Re:two enum questions

Jeff Kish < XXXX@XXXXX.COM >writes:
Quote
1 - I have a dll that has some functions declared extern "c".
One of the functions has a code passed in which tells the
function what to do.
I declared this, but had to comment out the declaration of the first
parameter of the function AddData, and use an int instead of the
enum. I thought it would be OK to use an enum, but it did not like the
declaration. Can someone explain what I'm doing wrong there?
You need to make sure that the size of the enum is the same in the dll
and in the exe that uses it. But since you didn't say what the
problem was, it's hard to know for sure if I'm on the right track or
not.
Quote
2 - I have three cpp files that include this header file, but the
linker is complaining about Public symbol '_TAnnotInsertOperation'
defined in both module ......
This one is easy. :)
Quote
#ifdef __DLL__
#define __EXPORT_TYPE __declspec(dllexport)
#else
#define __EXPORT_TYPE __declspec(dllimport)
#endif
You should not be defining macros with double-underscores in the name,
because they are reserved. By using the macro named
"__EXPORT_TYPE", you're needlessly causing undefined behavior in
your application.
Quote
#ifdef __cplusplus
extern "C" { /* Assume C declarations for C++ */
#endif /* __cplusplus */

enum TAnnotInsertOperation {aioAddMfr, aioAddManual,
aioAddNewPageForImage, aioAddMfrPart,
aioAssocItemMfrPart, aioAddMfrHotspot, aioAddItemAnnotNC,
aioAddGotoDocAnnotNC,
aioAddGotoAssetAnnotNC, aioAddgotoLocationAnnotNC}
TAnnotInsertOperation;
Notice, you're not only declaring a type, but also an instance of the
enum. Thus, every translation unit that includes this header will be
declaring the same variable again, and that is why the linker is
complaining. This is equivalent to declaring multiple global
variables with the same name in the same application.
Only declare the type here, then declare TAnnotInsertOperation as
having external linkage. That'll make the name known but not actually
declare the instance.
Then in one (and only one) .cpp file, declare your instance of
TAnnotInsertOperation that is not extern.
(Though avoiding global variables would also solve this problem, and
is a generally accepted "best practice" in programming.)
--
Chris (TeamB);
 

{smallsort}

Re:two enum questions

On Sat, 19 Nov 2005 17:09:27 -0500, Chris Uzdavinis (TeamB)
< XXXX@XXXXX.COM >wrote:
Quote
Jeff Kish < XXXX@XXXXX.COM >writes:

>1 - I have a dll that has some functions declared extern "c".
>One of the functions has a code passed in which tells the
>function what to do.
>I declared this, but had to comment out the declaration of the first
>parameter of the function AddData, and use an int instead of the
>enum. I thought it would be OK to use an enum, but it did not like the
>declaration. Can someone explain what I'm doing wrong there?

You need to make sure that the size of the enum is the same in the dll
and in the exe that uses it. But since you didn't say what the
problem was, it's hard to know for sure if I'm on the right track or
not.

OK I'll Look.
>2 - I have three cpp files that include this header file, but the
>linker is complaining about Public symbol '_TAnnotInsertOperation'
>defined in both module ......

This one is easy. :)


Notice, you're not only declaring a type, but also an instance of the
enum. Thus, every translation unit that includes this header will be
declaring the same variable again, and that is why the linker is
complaining. This is equivalent to declaring multiple global
variables with the same name in the same application.

Only declare the type here, then declare TAnnotInsertOperation as
having external linkage. That'll make the name known but not actually
declare the instance.

Then in one (and only one) .cpp file, declare your instance of
TAnnotInsertOperation that is not extern.

(Though avoiding global variables would also solve this problem, and
is a generally accepted "best practice" in programming.)
Thanks. should've seen that..
 

Re:two enum questions

On Fri, 18 Nov 2005 23:42:04 -0600, " Bruce Salzman"
< XXXX@XXXXX.COM >wrote:
Quote

"Jeff Kish" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>1 - I have a dll that has some functions declared extern "c".
>One of the functions has a code passed in which tells the
>function what to do.
>I declared this, but had to comment out the declaration of the first
>parameter of the function AddData, and use an int instead of the
>enum. I thought it would be OK to use an enum, but it did not like
>the
>declaration. Can someone explain what I'm doing wrong there?

Did you set the compiler options to treat enums as ints? If that
doesn't help, you can probably cast the enum to an int and back
without trouble.

I don't know.. I'll check in a few hours when I go back to work ;>)
Quote
>
>2 - I have three cpp files that include this header file, but the
>linker is complaining about Public symbol '_TAnnotInsertOperation'
>defined in both module ......
>I'm sure I must be doing something stupid to get this warning, but
>I'm
>not sure how to get rid of it besides not including the header in
>the
>other modules.. I'd appreciate any comments/tutorials/educational
>remarks.
>
>enum TAnnotInsertOperation {aioAddMfr, aioAddManual,
>aioAddNewPageForImage, aioAddMfrPart,
>aioAssocItemMfrPart, aioAddMfrHotspot, aioAddItemAnnotNC,
>aioAddGotoDocAnnotNC,
>aioAddGotoAssetAnnotNC, aioAddgotoLocationAnnotNC}
>TAnnotInsertOperation;

Get rid of that last TAnnotInsertOperation, i.e.

enum TAnnotInsertOperation {aioAddMfr, aioAddManual,
aioAddNewPageForImage, aioAddMfrPart,
aioAssocItemMfrPart, aioAddMfrHotspot, aioAddItemAnnotNC,
aioAddGotoDocAnnotNC,
aioAddGotoAssetAnnotNC, aioAddgotoLocationAnnotNC};
Thanks Bruce.