我試圖學習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=]
我無法看到任何答案中提到的基礎工具。下面是截圖:從書中例子
計劃:目標C(第3版)編程 - 第125
確切的錯誤信息會很有幫助。 –
我也嘗試過使用'#include'。它在前幾行提供了一串錯誤,說'缺少'('' –
用#import替換#include –