Objective C 中函数隐式声明的问题

发布于 2024-10-14 04:28:05 字数 1810 浏览 6 评论 0原文

我试图更多地了解 Objective-c,但目前我陷入了困境。我有 4 个错误,都是一样的。 “函数的隐式声明”,我用谷歌搜索但没有找到解决方案。

RadioStation .h

#import <Cocoa/Cocoa.h>
@interface RadioStation : NSObject {
  NSString* name;
  double frequency;
  char band;
}
+(double)minAMFrequency;
+(double)maxAMFrequency;
+(double)minFMFrequency;
+(double)maxFMFrequency;
-(id)initWithName:(NSString*)newName atFrequency:(double)newFrequency withBand:(char)newBand;
-(NSString*)name;
-(double)frequency;
-(char)band;
-(void)setName:(NSString*)newName;
-(void)setFrequency:(double)newFrequency;
-(void)setBand:(char)newBand;
@end

RadioStation .m

#import "RadioStation.h"

@implementation RadioStation
+(double)minAMFrequency{
 return 520.0;
};
+(double)maxAMFrequency{
     return 1610.0;
};
+(double)minFMFrequency{
 return 88.3;
};
+(double)maxFMFrequency{
 return 107.9;
};
-(id)initWithName:(NSString*)newName atFrequency:(double)newFrequency withBand:(char)newBand{
 self = [super init];
 if(self != nil){
  name = [newName retain];
  band = newBand;
  if (band == 'F') {
   if (newFrequency > maxFMFrequency()) {
    frequency = maxFMFrequency();
   }else if (newFrequency < minFMFrequency()) {
    frequency = minFMFrequency();
   }else {
    frequency = newFrequency;
   }

  }else if (band == 'A') {
   if (newFrequency > maxAMFrequency()) {
    frequency = maxAMFrequency();
   }else if (newFrequency < minAMFrequency()) {
    frequency = minAMFrequency();
   }else {
    frequency = newFrequency;
   }
  }
 }
 return self;
}
@end

这些行

if (newFrequency > maxFMFrequency()) {
if (newFrequency < minFMFrequency()) {
if (newFrequency > maxAMFrequency()) {
if (newFrequency < minAMFrequency()) {

都说“函数的隐式声明”

提前感谢, 迪格尔

I trying to learn a bit more about Objective-c, and at the moment i'm stuck. I got 4 errors, all the same. "Implicit declaration of function", I googled it but i didn't find a solution.

RadioStation .h

#import <Cocoa/Cocoa.h>
@interface RadioStation : NSObject {
  NSString* name;
  double frequency;
  char band;
}
+(double)minAMFrequency;
+(double)maxAMFrequency;
+(double)minFMFrequency;
+(double)maxFMFrequency;
-(id)initWithName:(NSString*)newName atFrequency:(double)newFrequency withBand:(char)newBand;
-(NSString*)name;
-(double)frequency;
-(char)band;
-(void)setName:(NSString*)newName;
-(void)setFrequency:(double)newFrequency;
-(void)setBand:(char)newBand;
@end

RadioStation .m

#import "RadioStation.h"

@implementation RadioStation
+(double)minAMFrequency{
 return 520.0;
};
+(double)maxAMFrequency{
     return 1610.0;
};
+(double)minFMFrequency{
 return 88.3;
};
+(double)maxFMFrequency{
 return 107.9;
};
-(id)initWithName:(NSString*)newName atFrequency:(double)newFrequency withBand:(char)newBand{
 self = [super init];
 if(self != nil){
  name = [newName retain];
  band = newBand;
  if (band == 'F') {
   if (newFrequency > maxFMFrequency()) {
    frequency = maxFMFrequency();
   }else if (newFrequency < minFMFrequency()) {
    frequency = minFMFrequency();
   }else {
    frequency = newFrequency;
   }

  }else if (band == 'A') {
   if (newFrequency > maxAMFrequency()) {
    frequency = maxAMFrequency();
   }else if (newFrequency < minAMFrequency()) {
    frequency = minAMFrequency();
   }else {
    frequency = newFrequency;
   }
  }
 }
 return self;
}
@end

The lines

if (newFrequency > maxFMFrequency()) {
if (newFrequency < minFMFrequency()) {
if (newFrequency > maxAMFrequency()) {
if (newFrequency < minAMFrequency()) {

all say "Implicit declaration of function"

Thanx in advance,
Dietger

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

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

发布评论

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

评论(4

等你爱我 2024-10-21 04:28:05

这些是类方法,因此您需要按如下方式更改它们:

if (newFrequency > [RadioStation maxFMFrequency]) {
if (newFrequency < [RadioStation minFMFrequency]) {
if (newFrequency > [RadioStation maxAMFrequency]) {
if (newFrequency < [RadioStation minAMFrequency]) {

These are class methods so you will need to change each of them as follows:

if (newFrequency > [RadioStation maxFMFrequency]) {
if (newFrequency < [RadioStation minFMFrequency]) {
if (newFrequency > [RadioStation maxAMFrequency]) {
if (newFrequency < [RadioStation minAMFrequency]) {
拥抱没勇气 2024-10-21 04:28:05

您混淆了方法和函数。

您调用该事物的代码

if (newFrequency > maxFMFrequency()) {

期望看到函数的声明,例如

double maxFMFrequency()

作为 C 函数实现的函数 - 这将无法从对象获取数据,因此您需要使用标头

确实声明了一个方法的方法,

+(double)maxFMFrequency;

但需要被称为

if (newFrequency > [RadioStation maxFMFrequency])

You are mixing up methods and functions.

the code you call the thing with

if (newFrequency > maxFMFrequency()) {

expects to see a declaration of a function like

double maxFMFrequency()

implemented as a C function - this will not be able to get data from the object so you need to use a methof

the header does declare a method as

+(double)maxFMFrequency;

but needs to be called as

if (newFrequency > [RadioStation maxFMFrequency])
め七分饶幸 2024-10-21 04:28:05

我认为这可能是因为你混合了 C 和 Objective C 语法。

尝试:

if (newFrequency > [self maxFMFrequency])

I think it might be because you're mixing C and Objective C syntax.

Try:

if (newFrequency > [self maxFMFrequency])
ゝ偶尔ゞ 2024-10-21 04:28:05

我遇到了同样的问题。通过修改函数调用进行排序,如下所示

//function declaration 
-(void) downloadPage:(NSString *)url;

//function definition 
-(void) downloadPage:(NSString *)url
{
userOutput.text = url;
}

//and now the fixed call to downloadPage

-(IBAction) onButtonOneClicked:(id) sender
{
userOutput.text = @"please wait...";
    //fixed call to downloadPage
[self downloadPage:[userInput text]];
}

I faced same problem. sorted by modifying the function call as given below

//function declaration 
-(void) downloadPage:(NSString *)url;

//function definition 
-(void) downloadPage:(NSString *)url
{
userOutput.text = url;
}

//and now the fixed call to downloadPage

-(IBAction) onButtonOneClicked:(id) sender
{
userOutput.text = @"please wait...";
    //fixed call to downloadPage
[self downloadPage:[userInput text]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文