I am trying to learn Objective-C and have written a small program but I cannot get it to compile. I get the following error message.
Uncaught exception NSInvalidArgumentException, reason: GSFFIInvocation: Class 'ClassA'(class) does not respond to forwardInvocation: for 'alloc'
The code is, as follows
#import <Foundation/Foundation.h>
@interface ClassA : NSObject
{
int numA;
}
- (void) setNum: (int) n;
- (int) getNum;
- (void) print;
@end
@interface ClassB : ClassA
{
char charB;
}
- (void) setChar: (char) c;
- (char) getChar;
- (id) init;
@end
@implementation ClassA
- (void) setNum: (int) n
{
numA = n;
}
- (int) getNum
{
return numA;
}
- (void) print
{
NSLog(@"ClassA num:%i ",numA);
}
@end
@implementation ClassB
- (void) setChar: (char) c
{
charB = c;
}
- (char) getChar
{
return charB;
}
- (void) print
{
NSLog(@"ClassB char:%c ", charB);
[super print];
}
- (id) init
{
numA = 1;
charB = 'c';
return self;
}
@end
int main(int argc, char* argv[])
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
ClassA* classA = [[ClassA alloc] init];
[classA setNum: 10];
//output: ClassA num: 10
[classA print];
//polymorphism example
ClassA* classB = [[ClassB alloc] init];
//prints: ClassB char: c ClassA num: 1
[classB print];
[classA release];
[classB release];
[pool drain];
return 0;
}
What am I doing wrong?