链接器/分析器错误

时间:2011-10-16 23:24:23

标签: objective-c xcode xcode4

我正在尝试学习Objective-C和我的程序(创建一个计算器)会得到我无法弄清楚的链接器或解析器错误。我不知道是什么原因导致了这个问题。我正在使用Xcode 4.1

#include <Foundation/Foundation.h>

@interface Calculator: NSObject
{
    double accumulator; 
}
// accumulator methods
- (void) setAccumulator: (double) value; 
- (void) clear;
-(double) accumulator;

// arithmetic methods
-(void) add: (double) value; 
-(void) subtract: (double) value; 
-(void) multiply: (double) value; 
-(void) divide: (double) value; 
@end

@implementation Calculator
-(void) setAccumulator: (double) value 
{
    accumulator = value; 
}
-(void) clear {
    accumulator = 0; 
}
-(double) accumulator {
    return accumulator; 
}
-(void) add: (double) value {
    accumulator += value; 
}
-(void) subtract: (double) value {
    accumulator -= value; 
}
-(void) multiply: (double) value {
    accumulator *= value; 
}
-(void) divide: (double) value {
    accumulator /= value; 
}
@end

int main (int argc, char *argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    double value1, value2;
    char operator;
    Calculator *deskCalc = [[Calculator alloc] init];
    NSLog (@"Type in your expression.");
    scanf ("%lf %c %lf", &value1, &operator, &value2);

    [deskCalc setAccumulator: value1]; 
    if ( operator == '+' )
        [deskCalc add: value2]; 
    else if ( operator == '-' )
        [deskCalc subtract: value2]; 
    else if ( operator == '*' )
        [deskCalc multiply: value2]; 
    else if ( operator == '/' )
        [deskCalc divide: value2];
    NSLog (@"%.2f", [deskCalc accumulator]); 
    [deskCalc release];
    [pool drain]; 
    return 0;
}

我猜它必须对标题做些什么?!?!?确切的错误消息是:

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h
/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:299:1: error: expected identifier or '(' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:301:19: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:302:44: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:304:19: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:305:43: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:307:19: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:307:50: error: unknown type name 'Protocol' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:308:19: error: unknown type name 'Protocol' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:308:50: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:312:30: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:312:53:{312:53-312:76}: error: format argument not an NSString [3]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:313:31: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:313:63:{313:63-313:86}: error: format argument not an NSString [3]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h
/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:8:1: error: expected identifier or '(' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:16:52: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:17:19: error: unknown type name 'NSString' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h
/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:1: error: expected identifier or '(' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:9:1: error: expected identifier or '(' [1]

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:13:1: error: expected identifier or '(' [1]

fatal error: too many errors emitted, stopping now [-ferror-limit=]

我无法看到答案中提到的基础工具。这是一个截图:

screenshot 书中的程序示例:Objective C中的编程(第3版) - Pg 125

4 个答案:

答案 0 :(得分:5)

您可能没有选择正确的Foundation项目模板:

proj template

因此,请检查您的项目中是否包含Foundation Framework:

从下面的第二张图片中,输入Foundation并选择“Foundation.framework”,然后再选择“Clean”和“Build”。

enter image description here

enter image description here

答案 1 :(得分:4)

要尝试的另一件事:将代码文件从* .cpp重命名为* .mm

答案 2 :(得分:2)

创建项目时,您选择C作为类型。请改为Foundation

答案 3 :(得分:0)

使用#import <Foundation/Foundation.h>

#import优于#include ...它保证导入的标头只引入一次。

确保您的项目框架中有“基础”。