2011-10-16 69 views
0

我試圖學習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 計劃:目標C(第3版)編程 - 第125

+1

確切的錯誤信息會很有幫助。 –

+1

我也嘗試過使用'#include '。它在前幾行提供了一串錯誤,說'缺少'('' –

+0

用#import替換#include –

回答

5

您可能沒有選擇正確的基金項目模板:

proj template

所以檢查基礎框架是在您的項目:

從下面的第二圖像,類型基金和選擇「Foundation.framework」,然後再次「清理」和「構建」。

enter image description here

enter image description here

2

當你創建的項目,您選擇C作爲類型。改爲嘗試Foundation

4

另一件事嘗試:從*的.cpp重命名你的代碼文件* .mm

0

使用#import <Foundation/Foundation.h>

#import優於#include ...它保證導入的標題僅被引入一次。

確保您的項目框架中有「基礎」。