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

发布于 2024-11-01 07:13:05 字数 1604 浏览 0 评论 0原文

大家好,我是编程新手,正在阅读 Objective-C 书籍来学习语言和编程基础知识。我反复查看了代码,回到了书中的示例,并试图理解 gcc 的完整错误。这是我的代码:

#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 编程》。

Hey all, I'm new to programming and going through an objective-c book to learn the language and programing fundamentals. I've looked through the code repeatedly, went back to the book's example, and attempted to understand the gcc comple errors. Here's my code:

#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;

    }

Then the compile errors from gcc look like this:

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’

Where am I going wrong?

btw I'm going through "Programming in Objective-C" by Stephen Kochan if you wanted to know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囚我心虐我身 2024-11-08 07:13:06

首先,基类应该是 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;
    }

write

@interface Point : NSObject
{
}

@property int xaxis;
@property int yaxis;

然后当您分配时您可以 write

[myPoint setXaxis:4]

myPoint.xaxis = 4;

当您释放对象时写 release,而不是 free

[myPoint release];

hth

First the base class should be NSObject, not Object

the normal way to do the initialization is to write the alloc and init in the same statement. You would typically have an -(id)init; method in your class:

-(id)init
{
  if ( ( self = [super init] ) )
  {
    ; // additional initialization goes here
  }
  return self;
}

and

int main (int argc, char *argv[])
    {
        Point *myPoint = [[Point alloc] init];

Properties are better used, then you get the setter and getter automatically generated for you

instead of

@interface Point: Object
    {
        int xaxis;
        int yaxis;
    }

write

@interface Point : NSObject
{
}

@property int xaxis;
@property int yaxis;

then when you assign you can either write

[myPoint setXaxis:4]

or

myPoint.xaxis = 4;

when you release the object write release, not free

[myPoint release];

hth

帥小哥 2024-11-08 07:13:06

您有警告和错误。这些警告似乎表明您正在子类化的 Object 未实现 allocinitfree 。通常,在 Apple 平台上,您可以对 NSObject 进行子类化,它确实实现了这些功能,但如果不知道您所在的平台,就不可能建议正确的选项。

其次,你有一个拼写错误,但现在似乎已得到纠正。这

point.m:40: error: ‘mypoint’ undeclared (first use in this function)

表明您的代码中有 mypoint,而不是 myPoint

You have warnings and an error. The warnings seem to suggest that Object, which you are subclassing, doesn't implement alloc, init or free. Normally, on an Apple platform, you'd subclass NSObject, which does implement these, but without knowing which platform you're on, it's not possible to advise the correct option.

Secondly, you had a typo, but that now seems to be corrected. This

point.m:40: error: ‘mypoint’ undeclared (first use in this function)

suggests that you had mypoint in your code, rather than myPoint.

爱本泡沫多脆弱 2024-11-08 07:13:06

您忘记包含标头 Foundation.h:

#import <Foundation/Foundation.h>

You forgot to include the header Foundation.h:

#import <Foundation/Foundation.h>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文