Проблема с неявным объявлением функции в цели c - PullRequest
4 голосов
/ 25 января 2011

Я пытаюсь узнать немного больше об 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()) {

все говорят "неявное объявление функции"

Спасибо заранее, Dietger

Ответы [ 4 ]

9 голосов
/ 25 января 2011

Это методы класса, поэтому вам нужно изменить каждый из них следующим образом:

if (newFrequency > [RadioStation maxFMFrequency]) {
if (newFrequency < [RadioStation minFMFrequency]) {
if (newFrequency > [RadioStation maxAMFrequency]) {
if (newFrequency < [RadioStation minAMFrequency]) {
3 голосов
/ 25 января 2011

Вы смешиваете методы и функции.

код, который вы вызываете с помощью

if (newFrequency > maxFMFrequency()) {

, ожидает увидеть объявление функции типа

double maxFMFrequency()

реализован как функция C - это не сможет получить данные от объекта, поэтому вам нужно использовать metaof

, заголовок действительно объявляет метод как

+(double)maxFMFrequency;

, но долженназываться

if (newFrequency > [RadioStation maxFMFrequency])
2 голосов
/ 25 января 2011

Я думаю, это может быть потому, что вы смешиваете синтаксис C и Objective C.

Попытка:

if (newFrequency > [self maxFMFrequency])
1 голос
/ 19 октября 2011

Я столкнулся с той же проблемой.отсортировано путем изменения вызова функции, как указано ниже

//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]];
}
...