【发布时间】:2011-12-08 22:32:45
【问题描述】:
我正在尝试学习 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=]
我看不到答案中提到的基础工具之类的东西。这是截图:
本书示例中的程序:Objective C 编程(第 3 版)-第 125 页
【问题讨论】:
-
确切的错误信息会有所帮助。
-
我也尝试过使用
#include <CoreFoundation/CoreFoundation.h>。它在前几行中给出了一堆错误说missing '(' -
用#import替换#include
-
配置错误,很明显。不过,不确定是什么配置错误。
-
@ott 导入也不起作用。我还没有配置任何东西。这是一个新的 Xcode 安装,并为项目选择了
command line tools。
标签: objective-c xcode xcode4