Copy-pasted code for the exercice from the “programing in objective-c” by Kochan from “Program 9.1”. But it doesn’t compile.
There are two classes: Fraction and Complex.
Complex’s objects seems to work fine, but Fraction’s object “fracResult” in “main.m” gives an error: “Assigning to “Fraction *” from incompatible type ‘void'”.
Here is Fraction.h:
#import <Foundation/Foundation.h>
// 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
Fraction.m file:
#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 NAN;
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
// add a Fraction to the receiver
-(void) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)
numerator = numerator * f.denominator + denominator * f.numerator;
denominator = denominator * f.denominator;
}
-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
@end
main.m file:
#import "Fraction.h"
#import "Complex.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *f1 = [[Fraction alloc] init];
Fraction *f2 = [[Fraction alloc] init];
Fraction *fracResult;
Complex *c1 = [[Complex alloc] init];
Complex *c2 = [[Complex alloc] init];
Complex *compResult;
[f1 setTo: 1 over: 10];
[f2 setTo: 2 over: 15];
[c1 setReal: 18.0 andImaginary: 2.5];
[c2 setReal: -5.0 andImaginary: 3.2];
// add and print 2 complex numbers
[c1 print]; NSLog (@" +"); [c2 print];
NSLog (@"---------");
compResult = [c1 add: c2];
[compResult print];
NSLog (@"\n");
[c1 release];
[c2 release];
[compResult release];
// add and print 2 fractions
[f1 print]; NSLog (@" +"); [f2 print];
NSLog (@"----");
fracResult= [f1 add: f2]; //this line gives an error
[fracResult print];
[f1 release];
[f2 release];
[fracResult release];
[pool drain];
return 0;
}
By researching on this topic, i found out that often, this type of error caused by incorrect use of pointers. It was suggested, to simply remove the asterisk sign from declaration and implementation of method “add:”. But that led to more “semantic issues”
Also, sometimes code from the book doesn’t compile because of difference in characters that were used in the book and that are used by xcode. Often, dash is different. But that also didn’t solve the issue with “incompatible type”.
Any advice or suggestion is welcome.
Problem is that
addis defined asbecause it adds the
Fraction* fpassed to the current object to the object itself. This means that it doesn’t return a new fraction that is the sum of the two fracion but it modifies the fraction itself. That’s why the return type is(void), otherwise it would have been-(Fraction*) add:(Fraction*)f;.So
tries to store the value returned by
[Fraction add:]but the return value isvoid, which is a non value and can’t be stored anywhere. (That’s why you get assigning from incompatible type void)