Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Oct 17, 2008, 10:51 PM   #1
macrumors newbie
Join Date: Oct 2008
programming in Objective-C

Hi there! I recently bought a book called "Programming in Objective C" by Stephen Kochan. I'm trying to do the examples in the book but they don't work well with XCode so I'm using Apple's Terminal program to do all the examples. Unfortunately, the following example I'm having major problems with and I'm hoping that someone will be kind enough to help and tell me where I'm going wrong and/or suggest what I ought to do to rectify this issue and possible future issues.

The example is taken from page 29 of the book and is example "Program 3.2". I've named the following program "poc1a.m"

#import <stdio.h>
#import <objc/Object.h>

// @interface section

@interface Fraction: Object
int numerator;
int denominator;

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


// @implementation section

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

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

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


// main program section

int main (int argc, char *argv[])
Fraction *myFraction

// create an instance of a fraction

myFraction = [Fraction alloc];
myFraction = [myFraction init];

// set fraction to 1/3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// display the fraction using print method

printf ("The value of myFraction is:");
[myFraction print];
printf ("\n");
[myFraction free];

return 0;

The code seems fine to me but unfortunately I'm getting quite a nasty error when I try to compile it. Incidentally, because the code (to compile the program) given by Kochan doesn't work for any of the examples in the book, I'm using a command line code I found on the internet. First please find the code by Kochan that doesn't work:

gcc poc1a.m -o poc1a objc

The command line code that I'm using is:

gcc -o poc1a poc1a.m

But using the above I get the following error:

Undefined symbols:
".objc_class_name_NSObject", referenced from:
.objc_class_name_Fraction in cclj29hr.o
"_objc_msgSend", referenced from:
_main in cclj29hr.o
_main in cclj29hr.o
_main in cclj29hr.o
_main in cclj29hr.o
_main in cclj29hr.o
_main in cclj29hr.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
olyagracheva-imac: poc olyagracheva $ gcc -o poc1a poc1a.m
poc1a.m:14: error: syntax error before ‘(’ token
poc1a.m:36: warning: incomplete implementation of class ‘Fraction’
poc1a.m:36: warning: method definition for ‘-setDenominator’ not found
poc1a.m: In function ‘main’:
poc1a.m:46: error: nested functions are disabled, use -fnested-functions to re-enable
poc1a.m:46: error: syntax error before ‘myFraction’
poc1a.m:47: error: ‘myFraction’ undeclared (first use in this function)
poc1a.m:47: error: (Each undeclared identifier is reported only once
poc1a.m:47: error: for each function it appears in.)

I'll be most grateful to you all for your help and advice.

Kind regards,

OlyaGracheva is offline   0 Reply With Quote
Old Oct 18, 2008, 01:27 AM   #2
macrumors 601
HiRez's Avatar
Join Date: Jan 2004
Location: Western US
You're missing a semicolon after the line:
Fraction *myFraction
and a colon in:
-(void) setDenominator (int) d;
Basically, you have typos, and there may be more. Check your punctuation.
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Oct 18, 2008, 03:13 AM   #3
macrumors member
Join Date: Aug 2008

I'm just finishing this book and the examples all work perfectly. What I would say though, is that I used NSObject as my superclass for all of the examples.

Also - it's worth reading through the Apple documentation before you start. It seems heavy in some parts - but it does help the understanding of Stephens book.
JonnyThunder is offline   0 Reply With Quote
Old Oct 18, 2008, 03:37 PM   #4
Thread Starter
macrumors newbie
Join Date: Oct 2008
Thank you all for your help and sound advice.

Yes there were a couple of typos in the program but I guess I was getting frustrated after having nearly tried everything and not getting the program to work - It was getting late and the version I'd put up was the 4th time I'd written it. Anyway, yes that was my fault.

Luckily I have found the cause of my problem and the reason why the program wasn't working. In Stephen Kochan's book, he tells us to compile the program using:

gcc main.m -o prog1 -l objc

What I did wrong was I mistook the "-l" for "-1" and so my commands weren't working correctly. I should have noticed this earlier. And so I believe the reason why the program didn't compile with the command that I'd put in was because I wasn't linking the program correctly with the Objective-C runtime library (objc).

Anyway, all works well now. Thank you all again for your help.

Kind regards,

OlyaGracheva is offline   0 Reply With Quote
Old Oct 18, 2008, 10:32 PM   #5
macrumors regular
Join Date: Aug 2005
Location: Canada
I have the same book, and I can confirm that the instructions given for XCode do work; and XCode can compile programs fine. In my printing, the instructions for XCode are on pages 10-16 and work for me. I still prefer to use the terminal however just as a personal preference. Maybe give it another try if you overlooked these pages.
Heath is offline   0 Reply With Quote
Old Oct 19, 2008, 09:02 AM   #6
macrumors regular
Join Date: Sep 2008
And here is the link to Kochan's book errata in case someone finds another possible problem.

A few minutes ago I was going through the example on page 367 and I spotted a strange piece of code.

if ([set1 isEqualToSet: set2] == NO)
   printf ("set1 equals set2\n");
Googling for Kochan isEqualToSet found that link above.
idelovski is offline   0 Reply With Quote
Old Oct 19, 2008, 10:36 AM   #7
macrumors regular
Join Date: Jul 2007
Location: Westerly RI
Great link idelovski! Thanks very much
15" Macbook pro 2.8Ghz iPhone 4 16 Gb iPod nano 4g 8Gb iPad 16Gb Canon EOS 7D
bunit is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Mac programming should be more like iOS programming. moonman239 Mac Programming 13 Feb 17, 2014 01:37 PM
Best book to learn Objective-C for a complete beginner to programming? Rasta4i iPhone/iPad Programming 7 Dec 18, 2013 09:03 AM
Best way to learn Objective C AppleDeviceUser iPhone/iPad Programming 23 Mar 6, 2013 11:57 PM
Printing With Objective C Jitin Jindal Mac Programming 12 Feb 28, 2013 12:01 PM
java to Objective-C spilakalb Alternatives to iOS and iOS Devices 0 Sep 30, 2012 11:17 PM

Forum Jump

All times are GMT -5. The time now is 05:17 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC