Error in person / employee inheritance?


#1

[code]#import <Foundation/Foundation.h>
#import “Employee.h”

int main(int argc, const char * argv[])
{

@autoreleasepool {
    Person * person = [[Employee alloc] init];
    //id person = [[Person alloc] init];
    //Person *person = [[Person alloc] init];
    [person setWeightInKilos:96];
    [person setHeightInMeters:1.8];
    float bmi=[person bodyMassIndex];
    NSLog(@"person (%d, %f) has a BMI of %f", [person weightInKilos], [person heightInMeters], bmi);
}
return 0;

}[/code]

Employee.h

[code]#import “Person.h”

@interface Employee : Person
{
int employeeID;
}
@property int employeeID;

@end[/code]

Employee.m

#import "Employee.h"

@implementation Employee
@synthesize employeeID;

@end

Person.h

[code]
#import <Foundation/Foundation.h>

@interface Person : NSObject
{
float heightInMeters;
int weightInKilos;
}

@property float heightInMeters;
@property int weightInKilos;

  • (float)bodyMassIndex;

@end[/code]

Person.m

[code]#import “Person.h”

@implementation Person

@synthesize heightInMeters,weightInKilos;

  • (float)bodyMassIndex
    {
    float h = [self heightInMeters];
    return [self weightInKilos] / (h*h);
    }

@end[/code]

This is returning a slew of errors "Use of undeclared identifier ‘person’"
First of all, I didn’t understand why there’s a space in the text “Person * person” or how that’s different from “Person *person”

I’ve checked a bunch of times and all the code looks correct… not sure what I’m missing!


#2

Resolved the issue, the files were not saved properly so outdated versions were being referred to. Once I deleted the files and resaved it worked again :cry:


#3

I have the same problems. I tried erased the files and reworked the chapter, just as you said you did, but still get the same error. Does anybody have any idea what I do wrong? Could it be that I cannot get Employee to be a subclass to Person? And how do I make sure that it is that way?

Thankful for help, am getting kinda frustrated…


#4

If you post your files, putting them beween the Code tags, I can have a look.


#5

Ok, so I solved the problem. Created a new project which I called BMITime2 and just copied everything from my previous BMITime and pasted it into this new project. Worked fine.

So weird, though.


#6

I found out what happened. I am posting this for others since it is near the top of the board.

#import <Foundation/Foundation.h>
#import "Person.h"

@interface Employee : Person

{
    int employeeID;
}

@property int employeeID;

@end

Note that in the @interface Employee : NSObject

should be @interface Employee : Person

Employee inherits from PERSON now and not NSObject