gcc在基本示例objc程序中编译错误

| 大家好,我是编程的新手,正在学习一本Objective-C书籍,以学习语言和编程基础知识。我反复浏览了一下代码,回到了本书的示例中,尝试理解gcc comple错误。这是我的代码:
#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’
我要去哪里错了? 顺便说一句,如果您想知道的话,我正在学习Stephen Kochan的“用Objective-C编程”。     
已邀请:
您有警告和错误。警告似乎暗示您正在子类化的
Object
没有实现
alloc
init
free
。通常,在Apple平台上,您会创建
NSObject
子类,该子类确实实现了这些
NSObject
,但是在不知道您所使用的平台上,无法建议正确的选择。 其次,您有错别字,但现在似乎已得到纠正。这个
point.m:40: error: ‘mypoint’ undeclared (first use in this function)
建议您的代码中包含
mypoint
,而不是
myPoint
。     
首先,基类应该是NSObject,而不是Object 进行初始化的通常方法是在同一条语句中编写alloc和init。您通常会有一个-(id)init;类中的方法:
-(id)init
{
  if ( ( self = [super init] ) )
  {
    ; // additional initialization goes here
  }
  return self;
}
int main (int argc, char *argv[])
    {
        Point *myPoint = [[Point alloc] init];
更好地使用属性,则可以自动为您生成setter和getter 代替
@interface Point: Object
    {
        int xaxis;
        int yaxis;
    }
@interface Point : NSObject
{
}

@property int xaxis;
@property int yaxis;
然后当您分配时,您可以写
[myPoint setXaxis:4]
要么
myPoint.xaxis = 4;
释放对象时,写释放,不是免费的
[myPoint release];
hth     
您忘记了包含标题Foundation.h:
#import <Foundation/Foundation.h>
    

要回复问题请先登录注册