2013-08-17 22 views
-3

嗨,我想學習目標C,我嘗試編寫與教程相同的代碼,但我無法運行該程序。生成失敗:預期的標識符錯誤

它說預計標識符爲NSAutoreleasePool * pool= [[NSAutoreleasePool alloc]]; 我的代碼中缺少的是什麼?

#import <Foundation/Foundation.h> 

    @interface Person : NSObject 
    { 
     int age; 
     int weight; 
    } 
    -(void) print; 
    -(void) setAge : (int) a; 
    -(void) setWeight : (int) w; 
    @end 

    @implementation Person 

    -(void) print{ 

     NSLog(@"His name is %i and his weight is %i" , age, weight); 

    } 
    -(void) setAge:(int)a { 
     age = a; 

    } 
    -(void) setWeight:(int)w { 
     weight=w; 

    } 

    @end 

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

     NSAutoreleasePool * pool= [[NSAutoreleasePool alloc]]; 
     Person *person; 

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

     [person setAge : 24]; 
     [person setWeight:90]; 

     [person print]; 
     [person release]; 
     [pool drain]; 
     return 0; 
} 


} 
+0

Alloc和init始終是嵌套的調用 - 永遠不會分離。這應該是學習的第一件事情之一(即總是執行[[object alloc] init];當然這也適用於您的人員對象。 – Mario

回答

0

您在外括號中缺少一條消息。更改爲:

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