I'm getting the following error:
"warning: conflicting types for '-(Fraction *) add: (Fraction *) f;"
with the line:
-(Fraction *) add: (Fraction *) f;
Here is my header code:
// Define the Fraction class
@interface Fraction : NSObject {
int numerator;
int denominator;
}
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) add: (Fraction *) f;
-(void) reduce;
@end
Here is my .m code:
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) print
{
NSLog (@"%i / %i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
-(void) reduce
{
开发者_运维问答 int u = numerator;
int v = denominator;
int temp;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
-(Fraction *) add: (Fraction *) f;
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b *d)
// multiply the denominators
// then mulitply each numerator by the OPPOSITE
// denominator
// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;
resultNum = numerator * f.denominator + denominator * f.numerator;
resultDenom = denominator * f.denominator;
[result setTo: resultNum over: resultDenom];
NSLog(@"%i, %i", resultNum, resultDenom);
[result reduce]; // reduces the numerator and denominator
// as both are instance (classwide) variables
NSLog(@"%i is the result", result);
return result;
}
@end
You have two different implementations of add:. One is:
-(void) add: (Fraction *) f;
inside the interface (.h file). The other is in the implementation (.m file)
-(Fraction *) add: (Fraction *) f;
You will need to change the header to read this latter declaration, so that it returns a Fraction pointer, not void.
That's because in your header file, you've declared the return type to be void:
-(void) add: (Fraction *) f;
Yet, in your implementation, you implement the method returning a Fraction pointer:
-(Fraction *) add: (Fraction *)
Also, you have a semicolon in your implementation, maybe just a copy-paste error?
You are declaring the add
method to return void
in your interface, but your implementation returns Fraction *
精彩评论