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.

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer

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

share|improve this answer

Make your constants names to be unique:

#define kHeading 1
#define kDirection 2
#define kStatement 3
#define kRefLink 4
#define kCorrectResponse 5
#define kIncorrect1Response 6
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.