Question

I'm new to Objective-C and working in GNUstep and MinGW environment. I am compiling this code but having an error:

#import "Foundation/Foundation.h"

@interface C : NSObject
{
    float f;
}

- (void) gamerHell: (NSString *) name : (NSString *) lastName ;

@end

@implementation C

- (void) gamerHell: (NSString *) firstName : (NSString *) lastName {

    NSLog(@"Welcome, %s %s",firstName,lastName);
}

@end

int main(int argc , const char * argv[]){

    NSAutoReleasePool * pool = [[NSAutoReleasePool alloc] init];

    C *ob = [[C alloc] init];
    [ob gamerHell: @"SHAN" : @"UL HAQ"];

    [ob release];

    [pool drain];
    return 0;
}

It is giving a compile-time error like this:

'NSAutoReleasePool' is undeclared (first use in this function)

What should I do to overcome this error?

Was it helpful?

Solution

Try using NSAutoreleasePool instead of NSAutoReleasePool (with a lowercase r).

OTHER TIPS

Adam nailed the problem you reported, you have a typo in the class name. However, there are a few other problems you'll run into that I figure it would help to know about.

  • Your method breaks several Objective-C conventions that will make your code less readable and confuse people trying to help you, namely:
    • Anonymous selector fragments are never a good idea. Always use a descriptive name before each colon.
    • A better signature would be - (void) hellowWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
    • I strongly suggest using the same formal parameter names in method declaration and definition. (You use "name" in one and "firstName" in the other.) If you choose to name them differently, be sure the one in the header file is well thought-out, since that's the public interface people will code to.
  • I'm assuming you've chosen a better class name than "C", and have just used that as a placeholder for demonstration purposes. Be especially cautious about class naming, since Objective-C doesn't have packages or namespaces for "uniqueing" classes.

I understand you're new to Objective-C, and these are all common afflictions for people just learning the language. Accordingly, please take these points as friendly advice, not harsh criticism.

switch from

NSAutoReleasePool * pool = [[NSAutoReleasePool alloc] init];

to

NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];

removing the space after the * took care of the error message for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top