PDA

View Full Version : Debugging Question




MountainDev
Oct 2, 2009, 04:00 PM
Anyone know why I am getting warnings on the following? The error seems to be coming from Fraction.M. I get two warning:

1. incomplete implemenation of class 'Fraction'
2. method definition for '-initwithNumerator:denominator:' not found.

Please help!


FRACTION.H

#import <Foundation/NSObject.h>

@interface Fraction: NSObject {
int numerator;
int denominator;
}

-(void) print;
-(Fraction*) initwithNumerator: (int) n denominator: (int) d;
-(void) setNumerator: (int) n andDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end

FRACTION.M

#import "Fraction.h"
#import <stdio.h>

@implementation Fraction
-(void) print {
printf( "%i/%i", numerator, denominator );
}

-(Fraction*) initWithNumerator: (int) n denominator: (int) d {
self = [super init];

if ( self ) {
[self setNumerator: n andDenominator: d];
}

return self;
}

-(void) setNumerator: (int) n andDenominator: (int) d {
numerator = n;
denominator = d;
}

//-(void) setNumerator: (int) n {
// numerator = n;
//}

//-(void) setDenominator: (int) d {
// denominator = d;
//}


-(int) numerator {
return numerator;
}

-(int) denominator {
return denominator;
}
@end

MAIN.M

#import <stdio.h>
#import "Fraction.h"

int main( int argc, const char *argv[] ) {
// create a new instance
Fraction *frac = [[Fraction alloc] initwithNumerator: 3 denominator: 7];

// print it
printf( "The fraction is: " );
[frac print];
printf( "\n" );

// free memory
[frac release];

return 0;
}



Catfish_Man
Oct 2, 2009, 04:19 PM
You have a capital W in the declaration of initWith... but call it with a lowercase w.

jared_kipe
Oct 2, 2009, 04:28 PM
Took me a while to spot, but method names are case sensitive.

Meaning initwithNumerator... is not the same as initWithNumerator...

You declared the later, but used the former in the main();


Thats one reason to love the Xcode's auto filling method name feature. Just start typing init and as long as xcode fills in the right one hit tab. I almost NEVER type in the full name for methods, in face almost never more than the first letter or two.

EDIT: Darn you Catfish_Man!!!

MountainDev
Oct 2, 2009, 04:42 PM
Thank you!