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

OlyaGracheva

macrumors newbie
Original poster
Oct 17, 2008
13
0
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;

@end

// @implementation section

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

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

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

@end

// 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,

Olya.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
You're missing a semicolon after the line:
Code:
Fraction *myFraction
and a colon in:
Code:
-(void) setDenominator (int) d;

Basically, you have typos, and there may be more. Check your punctuation.
 

JonnyThunder

macrumors member
Aug 16, 2008
67
0
Morning,

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.
 

OlyaGracheva

macrumors newbie
Original poster
Oct 17, 2008
13
0
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,

Olya
 

Heath

macrumors regular
Aug 19, 2005
133
0
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.
 

idelovski

macrumors regular
Sep 11, 2008
235
0
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.

Code:
if ([set1 isEqualToSet: set2] == NO)
   printf ("set1 equals set2\n");

Googling for Kochan isEqualToSet found that link above.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.