1

i'm currently learning objective-c by the "big nerd ranch"-book. I've repeated the code from chapter 18, but I'm getting errors. using 64bit macos 10.6 witch actual version of xcode.

compiler says:

Build target BMITime

CompileC /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.o BMITime/main.m normal x86_64 objective-c com.apple.compilers.llvm.clang.1_0.compiler cd /Users/pm/Dropbox/objective-c/BMITime setenv LANG en_US.US-ASCII /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch x86_64 -fmessage-length=0 -std=gnu99 -fobjc-arc -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wreturn-type -Wno-implicit-atomic-properties -Wformat -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wuninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-sign-compare -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wno-undeclared-selector -Wno-deprecated-implementations -DDEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -fasm-blocks -Wprotocol -Wdeprecated-declarations -mmacosx-version-min=10.7 -g -Wno-conversion -Wno-sign-conversion "-DIBOutlet=attribute((iboutlet))" "-DIBOutletCollection(ClassName)=attribute((iboutletcollection(ClassName)))" "-DIBAction=void)attribute((ibaction)" -iquote /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-generated-files.hmap -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-own-target-headers.hmap -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-all-target-headers.hmap -iquote /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/BMITime-project-headers.hmap -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Products/Debug/include -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/DerivedSources/x86_64 -I/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/DerivedSources -F/Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Products/Debug -include /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/PrecompiledHeaders/BMITime-Prefix-bbjacqhmgaerfrhblktcvkpzzntb/BMITime-Prefix.pch -MMD -MT dependencies -MF /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.d --serialize-diagnostics /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.dia -c /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m -o /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.o

/Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:21:10: error: no visible @interface for 'Employee' declares the selector 'setWeightInKilos:' [person setWeightInKilos:96]; ^ /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:22:10: error: no visible @interface for 'Employee' declares the selector 'setHeightInMeters:' [person setHeightInMeters:1.8]; ^ /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:26:22: error: no visible @interface for 'Employee' declares the selector 'bodyMassIndex' float bmi = [person bodyMassIndex]; ^ 3 errors generated.

my code looks like this:

main.m

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

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

    @autoreleasepool {

        // Erstellt eine Instanz von Person
        Employee *person = [[Employee alloc] init];

        // Gibt den Instanzvariablen interessante Werte
        [person setWeightInKilos:96];
        [person setHeightInMeters:1.8];
        [person setEmployeeID:15];

        // Ruft die Methode bodyMassIndex auf
        float bmi = [person bodyMassIndex];
        NSLog(@"Employee %d has a BMI of %f", [person employeeID], bmi);


    }
    return 0;
}

Person.h

#import <Foundation/Foundation.h>

// Die Klasse Person erbt alle Instanzvariablen
// und Methoden, die von der Klasse NSObject definiert wurden
@interface Person : NSObject
{
    // Sie hat zwei Instanzvariablen
    float heightInMeters;
    int weightInKilos;
}
@property float heightInMeters;
@property int weightInKilos;

// Diese Methode berechnet den Body-Mass-Index
- (float)bodyMassIndex;
@end

Employee.h

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

@interface Employee : NSObject
{
    int employeeID;
}
@property int employeeID;
@end

Employee.m

#import "Employee.h"

@implementation Employee

@synthesize employeeID;

@end

Person.m

#import "Person.h"

@implementation Person

@synthesize heightInMeters, weightInKilos;

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

@end
technergy
  • 259
  • 1
  • 12

1 Answers1

5

error: no visible @interface for 'Employee' declares the selector 'setWeightInKilos'
error: no visible @interface for 'Employee' declares the selector 'setHeightInMeters'
error: no visible @interface for 'Employee' declares the selector 'bodyMassIndex'

Don't have the book, but it would seem to me that Employee should inherit Person, not NSObject.

That would give Employee the missing properties/function by inheritance.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • This is exactly it. At the moment, Person and Employee are complete separate entities (both inherit from NSObject, but not from each other). As such, Employee class has no idea about weight/height/bmi. The fix would be to change `@interface Employee : NSObject` to `@interface Employee : Person` – WDUK Oct 21 '12 at 20:34