I get a warning which I cannot understand.
Given Fraction is a class, defined in "Fraction.h",
Help would be appreciated.
Thanks
Given Fraction is a class, defined in "Fraction.h",
Code:
//
// Fraction.h
// Chap 7 Prgm 7.1
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Fraction : NSObject {
int numerator;
int denominator;
}
@property int numerator, denominator;
/** class initialization **/
- initWith: (int) n over: (int) d;
-(void) reduce;
/* modified accessor*/
-(void) setTo: (int) n over: (int) d;
/*utilities*/
-(double) convertToNum;
-(void) print;
@end
Code:
//
// Fraction.m
// Chap 7 Prgm 7.1
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "Fraction.h"
#import "Comparison.h"
@implementation Fraction;
@synthesize denominator, numerator;
- initWith: (int) n over: (int) d
{
if ( ! ( self = [super init]))
return nil;
if (self)
[self setTo: n over: d];
return self;
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
/*********SPECIFIC TO EXERCISE 7-5 ***************
-(void) print
{
float f = 0.00;
if ( (f = (float) numerator / denominator) > 1)
NSLog(@"%i %i/%i", numerator/denominator, numerator % denominator, denominator);
else
NSLog(@"%i/%i ", numerator, denominator);
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
/*
-(Fraction *) add: (Fraction *) f
{
int resultNum, resultDenom;
Fraction* result = [[Fraction alloc] init];
resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;
[result setTo: resultNum over: resultDenom];
[result reduce];
return result;
}
*/
-(void) reduce
{
int u, v, temp;
BOOL isNegativeNumerator = NO;
BOOL isNegativeDenominator = NO;
if ( numerator < 0)
{
isNegativeNumerator = YES;
numerator = -numerator;
}
if ( denominator < 0)
{
isNegativeDenominator = YES;
denominator = -denominator;
}
u = numerator;
v = denominator;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
if ( isNegativeNumerator == YES || isNegativeDenominator == YES)
numerator = -numerator;
}
@end
Code:
#import "Fraction.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *fractArr[10], *fractPtr;
fractPtr = fractArr; /*warning: assignment from incompatible pointer type */
Fraction *aFrac = [ [ Fraction alloc] initWith: 3 over: 4];
Fraction *bFrac = [ [ Fraction alloc] initWith: 7 over: 8];
fractArr[0] = aFrac;
fractArr[1] = bFrac;
[fractPtr print];
fractPtr++;
[fractPtr print];
[aFrac release];
[bFrac release];
[pool drain];
return 0;
}
Help would be appreciated.
Thanks