2013-03-30 24 views
8

Tôi muốn gọi lớp C++ trong ViewController của mình. Vì vậy, tôi tạo ra một lớp như thế này: Hello.h"Loại bộ nhận cho các thông báo cụ thể là một khai báo chuyển tiếp" trong 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 

Nó hoạt động tốt trong p roject: http://files.cnblogs.com/cpcpc/Objective-C%E8%B0%83%E7%94%A8C.rar

Nhưng khi tôi sao chép mã vào dự án của tôi, nó xuất hiện "Loại bộ nhận cho thông báo cụ thể là lỗi khai báo chuyển tiếp".

Nếu tôi thay đổi "@class Hello;" để #import "Hello.h", nó xuất hiện "loại Unkwon lớp, bạn đã có nghĩa là Class" lỗi trong "class NewHello".

Tôi sử dụng xcode 4.6.Có ai giúp tôi không? Cảm ơn bạn!

Trả lời

18

Vấn đề là (như bạn đã nói) loại tệp cho ViewController.m là Obj-C và Hello.h là tệp Obj-C++. Giải pháp là để thêm

#import "Hello.h" 

đến file ViewController.m của bạn và thay đổi kiểu tập tin của ViewController.m để obj-C++ (từ bảng điều khiển bên phải)

+0

Nó hoạt động! Cảm ơn bạn rất nhiều! – Willen

+0

Cảm ơn nó đã giúp rất nhiều :) –

Các vấn đề liên quan