Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

87vert

macrumors 6502
Original poster
Oct 7, 2008
313
0
Pittsburgh, PA
Hey all,

I bought the Kochan book but I am having trouble with this code:

http://classroomm.com/objective-c/index.php?topic=683.0

It is for chp3 3.1 When I try to compile it I get 3962 errors of:

error: expected identifier or '(' before '-' token

They are all on:
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

Is something wrong with my xcode? I typed it in just like the book has listed and also tried a copy / paste from that site.

Any ideas?

My book is not the 2.0 version but I have the pdf of the 2.0 version. It looks like there are a lot of differences between the two. (Especially on this section) My book has it using the Object.h header and nothing about NSObject or NSLog. So I will go ahead and order the 2.0 book like everyone recommended.
 
What is the name and extension of the file you typed or pasted the code into?

What version of the OS and Xcode are you using?


Also, 3.1 in the copy/paste example you gave doesn't have:
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

Those are only present in 3.2.

Copy and paste your code into a reply, and post it here.
 
Sorry it is 3.2

OS 10.6.4 / Xcode 3.2.3

File name is main.c
Code:
#import <Foundation/Foundation.h>

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

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

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

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

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

@end

int main(int arcgc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	Fraction *myFraction;
	
	myFraction = [Fraction alloc];
	myFraction = [myFraction init];
	
	
	[myFraction setNumerator: 1];
	[myFraction setDenominator: 3];
	
	NSLog(@" The value fo myFraction is:");
	[myFraction print];
	[myFraction release];
	
	[pool drain];
	return 0;
}
 
I tried a new project and instead of Type: C, I selected Foundation (Chp3.m) and the build succeeds but debugger says No Launchable executable present at path. (I'm guess this is because the program is a command program and does not have an executable)

I dunno if this helps or not.
 
I am still learning myself but the one thing I noticed which may or may not solve your problem is this line

Code:
@implementation Fraction;

I think it should be without the semicolon like this

Code:
@implementation Fraction

I don't know if there are other mistakes but try that.
 
The filename needs to be main.m.

When a filename ends in ".c", it's compiled as plain C, not Objective-C. The extension for Objective-C is ".m".

Thanks, it looks like that is another difference between the two versions of the book.

I am going to order the correct book and start over.

Thanks again.
 
That book has a forum, where you can ask book-specific questions. I suggest that you register there, and describe the specific problem.

If you have the PDF of 2nd edition, use that. I don't know why you'd use the older edition in book form, when you have the later edition in PDF.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.