4

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

4 Answers 4

9

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]) {
Sign up to request clarification or add additional context in comments.

5 Comments

Thx, this was the solution for my prolem
Would the correct wording be "class methods" rather than "instance methods"?
@Dave, yip! I saw that I wrote static and "corrected" it without thinking, whoops. Good catch, thanks.
probably better to use [[self class] maxFMFrequency] etc. in case a subclass would override the class methods to return different constants.
Please mark this answer as the correct one. Or at least one that is correct.
3

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])

Comments

2

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

Try:

if (newFrequency > [self maxFMFrequency])

3 Comments

this returned an invalid operands to binary >
Hmm... Perhaps Mark or theChris could enlighten us as to why. It'll be worth a vote-up from me!
the problem is that when you access the method as if it were an instance method as you are doing above, it returns a pointer and the compiler assumes you want to do a binary operation on it. > is not a binary operator so it throws that error. I appreciate your effort, but this answer is NOT correct as stated by the OP in the comment above so not sure why it was picked. Must be your lucky day! :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.