0x0*_*0x0 0 xcode objective-c 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;
}
Run Code Online (Sandbox Code Playgroud)
我猜它必须用标题做点什么?!?!?确切的错误消息是:
/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=]
Run Code Online (Sandbox Code Playgroud)
我无法看到答案中提到的基础工具.这是一个截图:
书中的程序示例:Objective C中的编程(第3版) - 第125页
您可能没有选择正确的Foundation项目模板:

因此,请检查Foundation Framework是否包含在您的项目中:
从下面的第二张图片中,输入Foundation并选择"Foundation.framework",然后再选择"Clean"和"Build".


| 归档时间: |
|
| 查看次数: |
11234 次 |
| 最近记录: |