Objective C code

Joined
Feb 21, 2011
Messages
22
Reaction score
0
Points
1
Your Mac's Specs
Macbook Air 11", 64 GB, Intel core i5, 1.6 GHz, OSX Lion 10.7
Hello,

i just wrote a code to perform mathematical operations. the interface, implementation and main sections are written in the same file. after building and running, the code shows the following error.

Fraction.h no such file or directory
cannot find interface declaration for fraction

I tried to remove these errors by writing interface file again but its of no use. plz go through the code and let me know my mistake

Here's the code...

Code:
#import "Fraction.h"
@interface Fraction (MathOps)

-(Fraction *) add: (Fraction *) f;
-(Fraction *) mul: (Fraction *) f;
-(Fraction *) sub: (Fraction *) f;
-(Fraction *) div: (Fraction *) f;
@end

@implementation Fraction (MathOps)
-(Fraction *) add: (Fraction *) f
{
	
	// To add two fractions;
	// a/b + c/d = ((a*d) + (b*c)) / (b*d)

	Fraction *result = [[Fraction alloc] init];
	int resultNum, resultDenom;

	resultNum = (numerator * f.denominator) + (denominator * f.numerator);
	resultDenom = denominator * f.denominator;

	[result setTo: resultNum over: resultDenom];
	 [result reduce];

return result;
}
	 

	 -(Fraction *) sub: (Fraction *) f
	{

// To sub two fractions:
// a/b - c/d = ((a*d) - (b*c)) / (b*d)

		Fraction *result = [[Fraction alloc] init];
		int resultNum, resultDenom;

		resultNum = (numerator * f.denominator) - (denominator * f.numerator);
		resultDenom = denominator * f.denominator;

		[result setTo: resultNum over: resultDenom];
		[result reduce];
return result;
	}

-(Fraction *) mul: (Fraction *) f
{
	Fraction *result = [[Fraction alloc] init];

	[result setTo: numerator * f.numerator over: denominator * f.denominator];
	[result reduce];

return result;
}

-(Fraction *) div: (Fraction *) f
{
	Fraction *result = [[Fraction alloc] init];

	[result setTo: numerator * f.denominator over: denominator * f.numerator];
	[result reduce];

return result;
}
@end

int main (int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	Fraction *a = [[Fraction alloc] init];
	Fraction *b = [[Fraction alloc] init];
	Fraction *result;

	[a setTo: 1 over: 3];
	[b setTo: 2 over: 5];

	[a print];
	NSLog (@"	+");
	[b print];
	NSLog (@"-----");
	result = [a add: b];
	[result print];
	NSLog (@"\n");
	[result release];

	[a print];
	NSLog (@"	-");
	[b print];
	NSLog (@"-----");
	result = [a sub: b];
	[result print];
	NSLog (@"\n");
	[result release];

	[a print];
	NSLog (@"	*");
	[b print];
	NSLog (@"-----");
	result = [a mul: b];
	[result print];
	NSLog (@"\n");
	[result release];
	
	[a print];
	NSLog (@"	/");
	[b print];
	NSLog (@"-----");
	result = [a div: b];
	[result print];
	NSLog (@"\n");
	[result release];
	
	[a release];
	[b release];
	
	[pool drain];
	return 0;
}
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,762
Reaction score
2,100
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Since you put the interface, implementation and the Main function all in one file, why are you trying to "import" a header file?

You should move the interface definition into the Fraction.h file, and leave the implementation and Main section in the current file.

Regards
 
OP
P
Joined
Feb 21, 2011
Messages
22
Reaction score
0
Points
1
Your Mac's Specs
Macbook Air 11", 64 GB, Intel core i5, 1.6 GHz, OSX Lion 10.7
Hi RazOrEdge,

Thanks for your reply. I tried moving the interface and implementation section to other files but I am still getting the same errors

Here are the files:

FILE 1:

INTERFACE SECTION



#import <Foundation/Foundation.h>

@interface Fraction (MathOps)

-(Fraction *) add: (Fraction *) f;
-(Fraction *) mul: (Fraction *) f;
-(Fraction *) sub: (Fraction *) f;
-(Fraction *) div: (Fraction *) f;
@end






FILE 2:

IMPLEMENTATION SECTION


#import "Fraction.h"

@implementation Fraction (MathOps)
-(Fraction *) add: (Fraction *) f
{

// To add two fractions;
// a/b + c/d = ((a*d) + (b*c)) / (b*d)

Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * f.denominator) + (denominator * f.numerator);
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}


-(Fraction *) sub: (Fraction *) f
{

// To sub two fractions:
// a/b - c/d = ((a*d) - (b*c)) / (b*d)

Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * f.denominator) - (denominator * f.numerator);
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}

-(Fraction *) mul: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];

[result setTo: numerator * f.numerator over: denominator * f.denominator];
[result reduce];

return result;
}

-(Fraction *) div: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];

[result setTo: numerator * f.denominator over: denominator * f.numerator];
[result reduce];

return result;
}
@end






FILE 3:


MAIN SECTION


int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Fraction *a = [[Fraction alloc] init];
Fraction *b = [[Fraction alloc] init];
Fraction *result;

[a setTo: 1 over: 3];
[b setTo: 2 over: 5];

[a print];
NSLog (@" +");
[b print];
NSLog (@"-----");
result = [a add: b];
[result print];
NSLog (@"\n");
[result release];

[a print];
NSLog (@" -");
[b print];
NSLog (@"-----");
result = [a sub: b];
[result print];
NSLog (@"\n");
[result release];

[a print];
NSLog (@" *");
[b print];
NSLog (@"-----");
result = [a mul: b];
[result print];
NSLog (@"\n");
[result release];

[a print];
NSLog (@" /");
[b print];
NSLog (@"-----");
result = [a div: b];
[result print];
NSLog (@"\n");
[result release];

[a release];
[b release];

[pool drain];
return 0;
}
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top