基本示例 objc 程序中的 gcc 编译错误
大家好,我是编程新手,正在阅读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,基类应该是 NSObject,而不是 Object。
进行初始化的正常方法是将 alloc 和 init 写在同一个语句中。您通常会有一个 -(id)init;类中的方法:
而
最好使用属性,然后您会自动为您生成 setter 和 getter,
不是
write
然后当您分配时您可以 write
或
当您释放对象时写 release,而不是 free
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:
and
Properties are better used, then you get the setter and getter automatically generated for you
instead of
write
then when you assign you can either write
or
when you release the object write release, not free
hth
您有警告和错误。这些警告似乎表明您正在子类化的
Object
未实现alloc
、init
或free
。通常,在 Apple 平台上,您可以对NSObject
进行子类化,它确实实现了这些功能,但如果不知道您所在的平台,就不可能建议正确的选项。其次,你有一个拼写错误,但现在似乎已得到纠正。这
表明您的代码中有
mypoint
,而不是myPoint
。You have warnings and an error. The warnings seem to suggest that
Object
, which you are subclassing, doesn't implementalloc
,init
orfree
. Normally, on an Apple platform, you'd subclassNSObject
, 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
suggests that you had
mypoint
in your code, rather thanmyPoint
.您忘记包含标头 Foundation.h:
You forgot to include the header Foundation.h: