xcode 4.6中的“Receiver type for instance messages是一个前向声明”

时间:2013-03-30 14:36:39

标签: ios

我想在我的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

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

ViewController.h

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

@interface ViewController : UIViewController{
}
@end

ViewController.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

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

但是当我将代码复制到我的项目时,它显示“Receiver type for instance messages是一个前向声明”错误。

如果我改变“@class Hello;” #import“Hello.h”,在“NewHello类”中出现“Unkwon type class,你的意思是Class”错误。

我使用xcode 4.6。任何人都可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:22)

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

#import "Hello.h" 

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