Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

NSNick

macrumors regular
Original poster
Jun 27, 2008
162
0
Washington D.C.
Why do I get, "fatal error: method definition not in #implementation context" when I declare the methods in AppController.h, but not when I do not declare the methods?

#import <Cocoa/Cocoa.h>


@interface AppController : NSObject {
int fido;
}

@end

-(int)fido;
-(void)setFido:(int)x;


@implementation AppController

-(id)init
{
[super init];
[self setValue:[NSNumber numberWithInt:5]
forKey:mad:"fido"];
NSNumber *n = [self valueForKey:mad:"fido"];
NSLog(@"fido = %@",n);
return self;
}

-(int)fido
{
NSLog(@"-fido is returning %d", fido);
return fido;
}

-(void)setFido:(int)x
{
NSLog(@"-setFido: is called with %d", x);
fido = x;
}

@end
 

Darkroom

Guest
Dec 15, 2006
2,445
0
Montréal, Canada
you ended the inferface before declaring the methods

Code:
#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
int fido;
}

[COLOR="blue"]-(int)fido;
-(void)setFido:(int)x;[/COLOR]  [COLOR="SeaGreen"]//put them here[/COLOR]

@end

[COLOR="SeaGreen"]//not here[/COLOR]

and you forgot to import AppController.h in the implementation

Code:
[COLOR="Blue"]#import "AppController.h"[/COLOR]
@implementation AppController

-(id)init
{
[super init];
[self setValue:[NSNumber numberWithInt:5] forKey:@"fido"];
NSNumber *n = [self valueForKey:@"fido"];
NSLog(@"fido = %@",n);
return self;
}

-(int)fido
{
NSLog(@"-fido is returning %d", fido);
return fido;
}

-(void)setFido:(int)x
{
NSLog(@"-setFido: is called with %d", x);
fido = x;
}

@end
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
Because you need to have the method definitions before the @end


@interface AppController : NSObject {
int fido;
}

-(int)fido;
-(void)setFido:(int)x;

@end
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.