Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem With Macros (#define) "showing Expected identifier before numeric constant" error, in iPad

I am developing an application where i need to define several constants that will be used in more than one class.I have defined all my constants in one .h file(say "constants.h") and imported that file in myAppName_Prefix.pch file located in "Other sources" folder of the project.The classes using these constants are being compiled with out any error but other classes, where i declared some UISwipeGestureRecognizers, are throwing error as"Expected identifier before numeric constant" this is the snippet of code from one of the classes that is showing error:

if (gesture.direction==UISwipeGestureRecognizerDirectionLeft)

i defined my constants as:

#define heading 1
#define direction 2
#define statement 3
#define refLink 4
#define correctResponse 5
#define incorrect1Response 6

if i define them in each class individually then everything as working fine. Can any one please suggest me a way how to solve this issue.

like image 478
Hariprasad Avatar asked Mar 24 '11 12:03

Hariprasad


People also ask

Why are macros unsafe?

Malicious macros can do almost anything that other malware can do to your system, including emulating ransomware, stealing data, and emailing itself out to your contacts.

Can macros harm your computer?

A macro virus is a type of computer virus that's written in the same macro language as software programs, such as Microsoft Excel and Microsoft Word. Because a macro virus is written in the same language, it can not only infect your documents, but it can also damage your computer software.

Which errors can occur in macros?

Syntax errors occur when program statements do not conform to the rules of the macro language. Or, you may refer to a variable out of scope, causing a macro variable resolution error. Execution errors (also called semantic errors) are usually errors in program logic.


2 Answers

After preprocessing your code

if (gesture.direction==UISwipeGestureRecognizerDirectionLeft)

looks like this

if (gesture. 2==UISwipeGestureRecognizerDirectionLeft) 

and this is obviously not valid code.

The solution is to put an unique namespace string in front of your #defines.

#define hariDirection 2

or

#define kDirection 2

Or imho the best solution: don't use #define

typedef enum {
    heading = 1,
    direction,
    statement,
    refLink,
    correctResponse,
    incorrect1Response,
} MyDirection;

This will do the same thing, but it won't clash with other method and variable names.

like image 82
Matthias Bauch Avatar answered Sep 28 '22 11:09

Matthias Bauch


I was getting the same error message from gcc.

error: expected ')' before numeric constant
 #define UNIQUE_NAME 0

After checking that my variable names were unique, I realised that I had a typo at the point in the code where the constant was being used.

#define UNIQUE_NAME 0
//...
if (test_variable UNIQUE_NAME) { //missing ==
//...
}

simple mistake, but tricky to find because gcc was pointing me towards the #define statement

like image 34
craq Avatar answered Sep 28 '22 13:09

craq