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.
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.