ios xcode 4.6 中的“实例消息的接收器类型是前向声明”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15719681/
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-08-30 22:57:27  来源:igfitidea点击:

"Receiver type for instance messages is a forward declaration" in xcode 4.6

ios

提问by Willen

I want to call c++ class in my ViewController. So I create a class like this: Hello.h

我想在我的 ViewController 中调用 c++ 类。所以我创建了一个这样的类:Hello.h

#import <Foundation/Foundation.h>

@interface Hello : NSObject{
    class NewHello{
    private:int greeting_text;
    public:
        NewHello(){
            greeting_text=5;
        }
        void say_hello(){
            printf("Greeting_Text = %d",greeting_text);
        }
    };   
    NewHello *hello;
}
-(void)sayHellooooo;
@end

Hello.mm

你好.mm

#import "Hello.h"
@implementation Hello
-(void)sayHellooooo{
    hello = new NewHello();
    hello->say_hello();
}
@end

ViewController.h

视图控制器.h

#import <UIKit/UIKit.h>
//#include "Hello.h"
@class Hello;

@interface ViewController : UIViewController{
}
@end

ViewController.m

视图控制器.m

#import "ViewController.h"

@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
        NSLog(@"dddddddd");
    Hello *aa = [[Hello alloc]init];
    [aa sayHellooooo];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end

It works fine in the project:http://files.cnblogs.com/cpcpc/Objective-C%E8%B0%83%E7%94%A8C.rar

在项目中运行正常:http: //files.cnblogs.com/cpcpc/Objective-C%E8%B0%83%E7%94%A8C.rar

But when I copy the code to my project,it appears "Receiver type for instance messages is a forward declaration" error.

但是当我将代码复制到我的项目时,出现“实例消息的接收器类型是前向声明”错误。

If I change "@class Hello;" to #import "Hello.h",it appears "Unkwon type class,did you mean Class" error in "class NewHello".

如果我改变“@class Hello;” 到#import“Hello.h”,在“class NewHello”中出现“Unkwon type class,did you mean Class”错误。

I use xcode 4.6.Can anybody help me?Thank you!

我使用 xcode 4.6。有人可以帮助我吗?谢谢!

回答by Mar0ux

The problem is (as you've stated) file type for ViewController.m is Obj-C and Hello.h is a Obj-C++ file. The solution is to add

问题是(如您所说) ViewController.m 的文件类型是 Obj-C 而 Hello.h 是 Obj-C++ 文件。解决方法是添加

#import "Hello.h" 

to your ViewController.m file and change the file type of ViewController.m to Obj-C++ (from the right panel)

到您的 ViewController.m 文件并将 ViewController.m 的文件类型更改为 Obj-C++(从右侧面板)