#define and enum compiler error

I have two header files clashing.with #defines and enum usage

I have

#define USB_REQ_GET_STATUS 0

and in enum

USB_REQ_GET_STATUS = 0;

So the complier actually see
0 = 0 ;

and I get this error:

sketch/USB_protocol.h:71:3: note: in expansion of macro 'USB_REQ_GET_STATUS'
   USB_REQ_GET_STATUS = 0,
   ^
WEB_CAM_USB_Specification.h:31: error: expected unqualified-id before numeric constant
 #define  USB_REQ_GET_STATUS         0
                                     ^
sketch/USB_protocol.h:71:3: note: in expansion of macro 'USB_REQ_GET_STATUS'
   USB_REQ_GET_STATUS = 0,

So is usage of #define symbols wrong in enum?

Or is it OK but needs some more work ?

Thanks
Jim

Need to see Your code. What did you write versus what comes from libraries ?

You can't do that. It's like trying to give two variables the same name. You'll have to change one of them.

If you have two header files clashing, this can be a hard problem.

A possible fix is

#include <first file>

#undef USB_REQ_GET_STATUS

#include <second file>

But if your includes are clashing over a name as specific as that - it may be that they are trying to do the same job and are necessarily going to step on each other’s toes.

An alternative fix would be to break your sketch up into separate .cpp files, and have each of those only pull in the includes that it needs to do its job.

Both of these headers are different source 3rd part stuff and I did not actuals look WHERE / how the symbols used in enum are is defined.

USB_REQ_GET_STATUS = 0;

may not be a preprocessor symbol as I am assuming, but a real (enum ?) variable.

Jim

I don't see an enum in what you posted.

Please don't ask about compiler errors where you "loosely describe" code. Post the code in question.

Please use code tags.

Read this before posting a programming question

#define USB_REQ_GET_STATUS 0
and in enum
USB_REQ_GET_STATUS = 0;

Presumably you have an enum somewhere:

enum usb_commands {
USB_REQ_GET_STATUS = 0;
 ...  more
};

This is two separate ways of defining constants. Note that you don't REALLY have a problem, since both values are the same; pick one and get rid of the other. Offhand, I'd say that the "webcam" file has no business defining primitive USB constants, and should simply be including the USB_protocol.h file to get those definitions.

westfw:
Presumably you have an enum somewhere:

enum usb_commands {

USB_REQ_GET_STATUS = 0;
...  more
};




This is two separate ways of defining constants. Note that you don't REALLY have a problem, since both values are the same; pick one and get rid of the other. Offhand, I'd say that the "webcam" file has no business defining primitive USB constants, and should simply be including the USB_protocol.h file to get those definitions.

Thanks, I can see where I went wrong analyzing this issue.
Basically enum symbols cannot be used as #define symbols, if I remove the #defines I would have to change the code to use enum.
I cannot try it right now, but will do it later just to see the compiler output.

I appreciate your reply, It really helps to understand enum "definitions / symbols " in relations to #define.
Thanks again
Jim

julyjim:
Basically enum symbols cannot be used as #define symbols ...

Why not?

Basically enum symbols cannot be used as #define symbols ...

Why not?

Because "#define symbols" are converted by the pre-processor before the compiler gets to look at the code. You can do other variations, or you might have a different definition of "#define symbol", but that's the way I see it:

#define PREPROC_SYMBOL 0
#define PREPROC_SYM2 foo_bar

enum foo {
 ENUM_SYMBOL = PREPROC_SYMBOL;   /* OK */
 PREPROC_SYM2 = 1;   /* OK */
 PREPROC_SYM2 = PREPROC_SYMBOL;   /* OK */
 PREPROC_SYMBOL = 2;  /* NOT OK */
 ...  more
};

westfw:
Why not?
Because "#define symbols" are converted by the pre-processor before the compiler gets to look at the code. You can do other variations, or you might have a different definition of "#define symbol", but that's the way I see it:

#define PREPROC_SYMBOL 0

#define PREPROC_SYM2 foo_bar

enum foo {
ENUM_SYMBOL = PREPROC_SYMBOL;  /* OK /
PREPROC_SYM2 = 1;  /
OK /
PREPROC_SYM2 = PREPROC_SYMBOL;  /
OK /
PREPROC_SYMBOL = 2;  /
NOT OK */
...  more
};




There should not be semicolon after each "symbol line" , just comma. 

BTW is there a real / special name for symbols used in enum?

There should not be semicolon after each "symbol line" , just comma.

Oops; I should have looked up the exact syntax :frowning:

westfw:
Oops; I should have looked up the exact syntax :frowning:

I did same in OP, otherwise I would have not noticed it in your post..
!

westfw:
Why not?
Because "#define symbols" are converted by the pre-processor before the compiler gets to look at the code.

Yes, but that is a rather contrived example. If you have simple symbols you could use them interchangeably.

For example:

#define TEST1 1
#define TEST2 2
#define TEST3 3
#define TEST4 4
#define TEST5 5

Or:

enum {
  TEST1 = 1,
  TEST2 = 2,
  TEST3 = 3,
  TEST4 = 4,
  TEST5 = 5
};

The example given in the original post didn't look as if it was in the "complex" category:

#define USB_REQ_GET_STATUS  0

you could use them interchangeably.

Yes, but you can't do them BOTH...

Yes, but you can't do them BOTH...

Well, yes I agree. In much the same way you can't use the same variable name for different purposes inside your code. Nor can you do:

int foo = 42;
int foo = 42;

The OP said:

if I remove the #defines I would have to change the code to use enum.

I assumed he meant change where they were used. I don't see why such a change is necesssary. In my example above (reply #13) in either case you could write:

int foo = TEST1;

So he doesn't need to change that code.