Macro Problem (#define) "Shows expected identifier before numeric constant", on iPad

I am developing an application where I need to define several constants that will be used in more than one class. I defined all my constants in a single .h file (say, "constants.h") and imported this file into myAppName_Prefix.pch, located in the Other Sources folder of the project. Classes using these constants compiled with any error, but other classes where I declared some UISwipeGestureRecognizers throw an error like " Expected identifier before a numerical constant " is a piece of code from one of the classes that shows the 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 separately, then everything works fine. Can anyone suggest me a way to solve this problem.

+6
macros iphone ipad
source share
3 answers

After preprocessing the code

 if (gesture.direction==UISwipeGestureRecognizerDirectionLeft) 

as follows

 if (gesture. 2==UISwipeGestureRecognizerDirectionLeft) 

and this is obviously invalid code.

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

 #define hariDirection 2 

or

 #define kDirection 2 

Or imho better solution: don't use #define

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

This will do the same, but will not run into other method and variable names.

+4
source share

I was getting the same error message from gcc.

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

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

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

simple mistake, but hard to find because gcc was pointing to the #define

+2
source share

Make the constant names unique:

 #define kHeading 1 #define kDirection 2 #define kStatement 3 #define kRefLink 4 #define kCorrectResponse 5 #define kIncorrect1Response 6 
+1
source share

All Articles