嘿,我是新來的編程和通過一本Objective-C書來學習語言和編程基礎知識。我已經反覆查看代碼,回到本書的示例,並嘗試瞭解gcc comple錯誤。這裏是我的代碼:gcc在一個基本的例子objc程序中編譯錯誤
#import <stdio.h>
#import <objc/Object.h>
@interface Point: Object
{
int xaxis;
int yaxis;
}
-(void) print;
-(void) setx: (int)x;
-(void) sety: (int)y;
@end
@implementation Point;
-(void) print
{
printf("(%i,%i)", xaxis, yaxis);
}
-(void) setx: (int) x
{
xaxis = x;
}
-(void) sety: (int) y
{
yaxis = y;
}
@end
int main (int argc, char *argv[])
{
Point *myPoint;
myPoint = [Point alloc];
myPoint = [myPoint init];
[myPoint setx: 4];
[myPoint sety: 5];
printf("The coordinates are: ");
[myPoint print];
printf("\n");
[myPoint free];
return 0;
}
然後從GCC這個樣子的編譯錯誤:
urban:Desktop alex$ gcc point.m -o point -l objc
point.m: In function ‘main’:
point.m:38: warning: ‘Point’ may not respond to ‘+alloc’
point.m:38: warning: (Messages without a matching method signature
point.m:38: warning: will be assumed to return ‘id’ and accept
point.m:38: warning: ‘...’ as arguments.)
point.m:40: error: ‘mypoint’ undeclared (first use in this function)
point.m:40: error: (Each undeclared identifier is reported only once
point.m:40: error: for each function it appears in.)
point.m:49: warning: ‘Point’ may not respond to ‘-free’
我要去哪裏錯了?
btw如果你想知道,我正在通過Stephen Kochan的「Objective-C編程」。
在哪個平臺上編譯程序? – 2011-04-13 06:58:21
我正在使用Mac OS X. 10。6 – imgeekboy 2011-04-13 07:38:19
在這種情況下,最好使用'NSObject'而不是'Object',因爲Mac OS X上的股票編譯器使用Objective-C 2.0,它不會將Object作爲全功能的根類來構建。 – 2011-04-13 07:42:27