0

我目前正在通过“大书呆子牧场”一书学习objective-c。我重复了第 18 章中的代码,但我遇到了错误。使用 64 位 macos 10.6 巫婆实际版本的 xcode。

编译器说:

构建目标 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 目标- 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-缺少原型 -Wreturn-type -Wno-implicit-atomic-properties -Wformat -Wno-missing-braces -W括号 -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-选择器 -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=属性((iboutlet))" "-DIBOutletCollection(ClassName)=属性((iboutletcollection(ClassName)))" "-DIBAction=void)属性((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/中间体/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-前缀。pch -MMD -MT 依赖项 -MF /Users/pm/Library/Developer/Xcode/DerivedData/BMITime-cjqpmtvlmdoeiecyxbofznzjgpij/Build/Intermediates/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.d --序列化诊断 /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.obuild/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/构建/中间体/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.obuild/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/构建/中间体/BMITime.build/Debug/BMITime.build/Objects-normal/x86_64/main.o

/Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:21:10: 错误:'Employee' 没有可见的@interface 声明选择器'setWeightInKilos:' [person setWeightInKilos:96];^ /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:22:10: 错误:'Employee' 没有可见的@interface 声明选择器'setHeightInMeters:' [person setHeightInMeters:1.8];^ /Users/pm/Dropbox/objective-c/BMITime/BMITime/main.m:26:22: 错误:'Employee' 没有可见的@interface 声明选择器'bodyMassIndex' float bmi = [person bodyMassIndex]; ^ 产生 3 个错误。

我的代码如下所示:

主文件

#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;
}

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

员工.h

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

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

员工.m

#import "Employee.h"

@implementation Employee

@synthesize employeeID;

@end

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

1 回答 1

4

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.

于 2012-10-21T20:31:32.230 回答