spandana
spandana

Reputation: 755

getting errors using macros in Objective-C

I am new to Objective-C. I was trying out a sample program using macros and getting errors.

#import <Foundation/Foundation.h>

#define HELLO_WORLD @"Hello World"

#define a(x,y) ((x)+(y))

#define PRINTMAC(x,y)\
NSLog(@"%d",a((x),(y));\

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    //NSLog(@"%d",add(10,20));
    PRINTMAC(13,72);  //error:
    [pool drain];
    return 0;
} //error:

Error: expected ';' before '}' token

Upvotes: 1

Views: 59

Answers (1)

Jason Musgrove
Jason Musgrove

Reputation: 3583

You appear to be missing a ) on the NSLog line (line 8).

Additionally, I'm not sure you need the final \ on that line, as the macro is not being carried on to a third line.

Finally, I don't think you need the ; on that line either as it, combined with the semi-colon when you invoke the macro on line 15 results in an empty statement (shouldn't be harmful, though).

Upvotes: 4

Related Questions