开发者

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

开发者 https://www.devze.com 2023-02-19 17:07 出处:网络
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 f

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开发者_StackOverflow社区==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.


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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号