Help with Methods - Kochan Book

Discussion in 'Mac Programming' started by 87vert, Aug 27, 2010.

  1. 87vert macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #1
    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.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. 87vert thread starter macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #3
    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;
    }
    
    
     
  4. 87vert thread starter macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #4
    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.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    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".
     
  6. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    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.
     
  7. 87vert thread starter macrumors 6502

    87vert

    Joined:
    Oct 7, 2008
    Location:
    Pittsburgh, PA
    #7
    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.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    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.
     

Share This Page