2013-03-30 27 views
8

我想在我的ViewController中調用C++類。所以我創建這樣一個類: Hello.h「接收器類型,例如消息是一個前向聲明」在xcode 4.6

#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 

它在p中工作正常roject:http://files.cnblogs.com/cpcpc/Objective-C%E8%B0%83%E7%94%A8C.rar

但是,當我將代碼複製到我的項目,它顯示「接收器類型的實例消息是前向聲明」錯誤。

如果我更改「@class Hello」到#import「Hello.h」,在「NewHello類」中出現「Unkwon類型類,是否指Class」錯誤。

我使用xcode 4.6.任何人都可以幫助我嗎?謝謝!

回答

18

問題是(如你所述)ViewController.m的文件類型是Obj-C,而Hello.h是Obj-C++文件。解決的辦法是

#import "Hello.h" 

添加到您的ViewController.m文件並更改ViewController.m到obj-C++的文件類型(從右圖)

+0

它的工作原理!非常感謝您! – Willen

+0

感謝它幫助了很多:) –

相關問題