Lan*_*opp 44 c++ objective-c objective-c++
我正在尝试将Objective-C与C++混合使用.当我编译代码时,我得到了几个错误.
啊
#import <Cocoa/Cocoa.h>
#include "B.h"
@interface A : NSView {
    B *b;
}
-(void) setB: (B *) theB;
@end
上午
#import "A.h"
@implementation A
- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}
- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
}
-(void) setB: (B *) theB {
    b = theB;
}
@end
BH
#include <iostream>
class B {
    B() {
        std::cout << "Hello from C++";
    }
};
以下是错误:
/Users/helixed/Desktop/Example/B.h:1:0 /Users/helixed/Desktop/Example/B.h:1:20: error: iostream: No such file or directory
/Users/helixed/Desktop/Example/B.h:3:0 /Users/helixed/Desktop/Example/B.h:3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'B'
/Users/helixed/Desktop/Example/A.h:5:0 /Users/helixed/Desktop/Example/A.h:5: error: expected specifier-qualifier-list before 'B'
/Users/helixed/Desktop/Example/A.h:8:0 /Users/helixed/Desktop/Example/A.h:8: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:26:0 /Users/helixed/Desktop/Example/A.m:26: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:27:0 /Users/helixed/Desktop/Example/A.m:27: error: 'b' undeclared (first use in this function)
Pab*_*ruz 77
你需要命名你的.m文件.mm.您将能够使用Objective-C编译C++代码.
因此,按照您的示例,您的AView.m文件应命名为AView.mm.这很简单.它工作得很好.我在iPhone项目中使用了很多std容器(std :: vector,std :: queue等)和遗留的C++代码而没有任何复杂性.
没关系,我觉得很蠢.您所要做的就是将AView.m重命名为AView.mm,以便编译器知道它的Objective-C++,并且编译没有问题.
| 归档时间: | 
 | 
| 查看次数: | 42652 次 | 
| 最近记录: |