简体   繁体   中英

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

I want to call c++ class in my ViewController. So I create a class like this: 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

It works fine in the project: 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".

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

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

#import "Hello.h" 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM