我正在尝试将Objective-C与C++混合使用.当我编译代码时,我得到了几个错误.
啊
#import <Cocoa/Cocoa.h>
#include "B.h"
@interface A : NSView {
B *b;
}
-(void) setB: (B *) theB;
@end
Run Code Online (Sandbox Code Playgroud)
上午
#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
Run Code Online (Sandbox Code Playgroud)
BH
#include <iostream>
class B {
B() {
std::cout << "Hello from C++";
}
};
Run Code Online (Sandbox Code Playgroud)
以下是错误:
/Users/helixed/Desktop/Example/B.h:1:0 /Users/helixed/Desktop/Example/B.h:1:20: …Run Code Online (Sandbox Code Playgroud) 当我从C++调用NSLog时,Xcode抱怨传递给NSLog的格式字符串不是文字字符串.这是触发警告的一行代码:
NSLog(CFSTR("Leaking?"));
Run Code Online (Sandbox Code Playgroud)
我不知道的任何方式代码C++文字的NSString,我没有看到相关的警告,我可以在项目设置中关闭.有没有办法从C++调用NSLog而不触发此消息?我正在使用Xcode 4.2.1.
编辑:这真的是C++代码.我通常避免使用Objective-C++,坚持使用Objective-C或普通的旧C++,因为没有官方文档说明Objective-C++中哪些有用,哪些没有.我只发现模糊的警告(例如)STL的某些部分可能存在问题.我使用模板,STL和C++的其他"高级"功能,所以我想安全地玩它.
编辑#2,解决方案:我只是想通了clang支持的警告标志比实际记录的要多得多.(从Xcode提供给我的长长的警告列表中可以看出这一点.)我尝试了-Wno-format-nonliteral a la gcc,现在Xcode很高兴.