Jeel Shah
Jeel Shah

Reputation: 3324

Objective C program not compiling

I am currently learning objective c on my pc and my program will not compile. This is the error that i am getting. "Interface.m: In function '-[Person print]':Interface.m:17:2:error: cannot find intergace declartation for 'NXConstantString'"

I am using the gcc compiler.

Here is my program

 #import <Foundation/NSObject.h>
 #import <stdio.h>

@interface Person : NSObject {
    int age;
    int weight;
}

-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;

@end    

@implementation Person
-(void) print {
    printf(@"I am %i years old and I weigh about %i pounds",age,weight);
}
-(void) setAge: (int) a{
    age = a;
}
-(void) setWeight: (int) w{
    weight = w;
}
@end 

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

    Person *person;

    person = [Person alloc];    
    person = [person init];

    [person setAge: 16];
    [person setWeight: 120];
    [person print];
    [person release];

    return 0;

}

Upvotes: 2

Views: 182

Answers (1)

user557219
user557219

Reputation:

Literal strings such as @"I am %i years old and I weigh about %i pounds" are (by default) of type NSConstantString but you’re not importing the header file that declares that class.

You could either add:

#import <Foundation/NSString.h>

or simply import all headers in the Foundation framework:

#import <Foundation/Foundation.h>

Edit: I’ve just noticed that you’re using an Objective-C string as an argument to printf():

printf(@"I am %i years old and I weigh about %i pounds",age,weight);

That’s not right; printf() expects a C string, e.g.:

printf("I am %i years old and I weigh about %i pounds",age,weight);

You could also use NSLog(), which does expect an Objective-C string:

NSLog(@"I am %i years old and I weigh about %i pounds",age,weight);

Upvotes: 3

Related Questions