xcode 链接器/解析器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7788070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:09:07  来源:igfitidea点击:

Linker/Parser error

objective-cxcodexcode4

提问by 0x0

I'm trying to learn Objective-C and my program (creating a calculator) gets linker or parser error that I'm not able to figure out. I don't know what can cause this problem. I'm using Xcode 4.1

我正在尝试学习 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;
}

I'm guessing it has to do something with the header?!?!? the exact error messages are :

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

/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=]

I'm not able to see anything like a foundation tool as mentioned in the answers. Here is a screenshot :

我看不到答案中提到的任何类似基础工具的东西。这是一个屏幕截图:

screenshotProgram from the book example : Programming in Objective C (3rd edition) - Pg 125

截屏本书示例中的程序:Objective C 中的编程(第 3 版)- 第 125 页

回答by chown

You may not have chose the correct Foundation project template:

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

proj template

项目模板

So check that the Foundation Framework is included in your project:

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

From the second image below, type in Foundation and select "Foundation.framework", then "Clean" and "Build" again.

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

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by electromaggot

Another thing to try: rename your code file from *.cpp to *.mm

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

回答by Jim

When you created the project, you chose Cas the type. Try Foundationinstead.

创建项目时,您选择C了类型。试试吧Foundation

回答by TJez

Use #import <Foundation/Foundation.h>

#import <Foundation/Foundation.h>

#importis better than #include... it guarantees that the imported header is introduced only once.

#import#include...更好,它保证导入的标头只引入一次。

Make sure you have "Foundation" in your project frameworks.

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