2

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;

}

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.