0

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?

bbum
  • 162,346
  • 23
  • 271
  • 359
basil
  • 690
  • 2
  • 11
  • 30

1 Answers1

1

If you aren't working with Xcode, it is important to tag your question appropriately.

It would appear that the implementation of NSObject that you're using does not implement +alloc or the runtime isn't seeing the implementation of said method likely due to a linking issue.

I.e. something is screwed up or non-standard in your installation.

bbum
  • 162,346
  • 23
  • 271
  • 359