作者热门文章
- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我想在我的 ViewController 中调用 C++ 类。所以我创建了一个这样的类:你好.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
你好.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 is a forward declaration”错误。
如果我改变“@class Hello;” #import "Hello.h",在"class NewHello"中出现"Unkwon type class, did you mean Class"错误。
我使用的是 xcode 4.6。有人能帮帮我吗?谢谢!
最佳答案
问题是(如您所述)ViewController.m 的文件类型是 Obj-C 而 Hello.h 是 Obj-C++ 文件。解决方案是添加
#import "Hello.h"
到您的 ViewController.m 文件并将 ViewController.m 的文件类型更改为 Obj-C++(从右侧面板)
关于ios - xcode 4.6 中的 "Receiver type for instance messages is a forward declaration",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15719681/
我是一名优秀的程序员,十分优秀!