Objective-C on GNUstep AutoReleasePool 未声明的问题

发布于 2024-08-02 04:59:46 字数 734 浏览 9 评论 0原文

我是 Objective-C 的新手,在 GNUstep 和 MinGW 环境中工作。我正在编译此代码,但出现错误:

#import "Foundation/Foundation.h"

@interface C : NSObject
{
    float f;
}

- (void) gamerHell: (NSString *) name : (NSString *) lastName ;

@end

@implementation C

- (void) gamerHell: (NSString *) firstName : (NSString *) lastName {

    NSLog(@"Welcome, %s %s",firstName,lastName);
}

@end

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

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

    C *ob = [[C alloc] init];
    [ob gamerHell: @"SHAN" : @"UL HAQ"];

    [ob release];

    [pool drain];
    return 0;
}

它给出如下编译时错误:

“NSAutoReleasePool”未声明(在此函数中首次使用)

我应该怎么做才能克服此错误?

I'm new to Objective-C and working in GNUstep and MinGW environment. I am compiling this code but having an error:

#import "Foundation/Foundation.h"

@interface C : NSObject
{
    float f;
}

- (void) gamerHell: (NSString *) name : (NSString *) lastName ;

@end

@implementation C

- (void) gamerHell: (NSString *) firstName : (NSString *) lastName {

    NSLog(@"Welcome, %s %s",firstName,lastName);
}

@end

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

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

    C *ob = [[C alloc] init];
    [ob gamerHell: @"SHAN" : @"UL HAQ"];

    [ob release];

    [pool drain];
    return 0;
}

It is giving a compile-time error like this:

'NSAutoReleasePool' is undeclared (first use in this function)

What should I do to overcome this error?

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

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

发布评论

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

评论(3

抱着落日 2024-08-09 04:59:46

尝试使用 NSAutoreleasePool 而不是 NSAutoReleasePool (使用小写的 r)。

Try using NSAutoreleasePool instead of NSAutoReleasePool (with a lowercase r).

笑叹一世浮沉 2024-08-09 04:59:46

亚当解决了您报告的问题,您的类名有一个拼写错误。但是,您还会遇到一些其他问题,我认为了解这些问题会有所帮助。

  • 你的方法打破了一些 Objective-C 约定,这些约定会降低你的代码的可读性,并使试图帮助你的人感到困惑,即:
    • 匿名选择器片段从来都不是一个好主意。始终在每个冒号之前使用描述性名称。
    • 更好的签名是 - (void) hellowWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
    • 我强烈建议在方法声明和定义中使用相同的形式参数名称。 (您在一个中使用“name”,在另一个中使用“firstName”。)如果您选择以不同的方式命名它们,请确保头文件中的名称经过深思熟虑,因为这是人们将编码的公共接口。< /里>
  • 我假设您选择了一个比“C”更好的类名,并且只是将其用作演示目的的占位符。对于类命名要特别小心,因为 Objective-C 没有用于“唯一”类的包或命名空间。

我知道您是 Objective-C 的新手,这些都是刚学习这门语言的人常见的烦恼。因此,请将这些观点视为友好的建议,而不是严厉的批评。

Adam nailed the problem you reported, you have a typo in the class name. However, there are a few other problems you'll run into that I figure it would help to know about.

  • Your method breaks several Objective-C conventions that will make your code less readable and confuse people trying to help you, namely:
    • Anonymous selector fragments are never a good idea. Always use a descriptive name before each colon.
    • A better signature would be - (void) hellowWithFirstName:(NSString*)firstName lastName:(NSString*)lastName;
    • I strongly suggest using the same formal parameter names in method declaration and definition. (You use "name" in one and "firstName" in the other.) If you choose to name them differently, be sure the one in the header file is well thought-out, since that's the public interface people will code to.
  • I'm assuming you've chosen a better class name than "C", and have just used that as a placeholder for demonstration purposes. Be especially cautious about class naming, since Objective-C doesn't have packages or namespaces for "uniqueing" classes.

I understand you're new to Objective-C, and these are all common afflictions for people just learning the language. Accordingly, please take these points as friendly advice, not harsh criticism.

凤舞天涯 2024-08-09 04:59:46

从切换

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

到删除空格。

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

在 * 为我处理错误消息后,

switch from

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

to

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

removing the space after the * took care of the error message for me.

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