当我尝试整合来自SpeakHere的记录/播放代码时,我的编译器报告了很多错误。 我已经遵循建议使用.mm文件来包含cpp包含文件,唯一的例外是我使用启用了ARC的iOS 5。
我的代码是:
//header file
#import "AQPlayer.h"
#import "AQRecorder.h"
@interface MyClass : NSObject {
UIButton *record;
UIButton *cancel;
UIButton *stop;
UIButton *play;
//
AQPlayer* player;
AQRecorder* recorder;
BOOL playbackWasInterrupted;
BOOL playbackWasPaused;
CFStringRef recordFilePath;
}
@property (nonatomic, retain) IBOutlet DrawerAudioNote *dnote;
@property (nonatomic, retain) IBOutlet UIButton *record;
@property (nonatomic, retain) IBOutlet UIButton *cancel;
@property (nonatomic, retain) IBOutlet UIButton *stop;
@property (nonatomic, retain) IBOutlet UIButton *play;
- (IBAction) doRecord:(id)sender;
- (IBAction) doStop:(id)sender;
- (IBAction) doCancel:(id)sender;
- (IBAction) doPlay:(id)sender;
//
@property (readonly) AQPlayer *player;
@property (readonly) AQRecorder *recorder;
//.mm file (no compile error)
@import "MyClass.h"
@implementation MyClass
@synthesize record, cancel, stop, play;
@synthesize playbackWasInterrupted;
@synthesize player, recorder;
//CAXException.h (an example cpp file with compile errors)
......
class CAX4CCString {
//ERROR: Expect ';' after top level declarator
//ERROR: Unknown type name; do you mean 'Class'?
public:
CAX4CCString(OSStatus error) {
// see if it appears to be a 4-char-code
char *str = mStr;
*(UInt32 *)(str + 1) = CFSwapInt32HostToBig(error);
if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint(str[4])) {
str[0] = str[5] = '\'';
str[6] = '\0';
} else if (error > -200000 && error < 200000)
// no, format it as an integer
sprintf(str, "%d", (int)error);
else
sprintf(str, "0x%x", (int)error);
}
const char *get() const { return mStr; }
operator const char *() const { return mStr; }
private:
char mStr[16];
};
通过类'CAX4CCString'中的编译错误,编译器似乎不理解他正在处理CPP文件。 我知道我会错过任何地方的东西,任何人都可以指出?
提前致谢,
肖恩
答案 0 :(得分:2)
为什么不为特定文件禁用ARC?您的项目仍然使用ARC,但有问题的文件除外。将-fno-objc-arc
添加到您不希望使用ARC的文件的编译器标志中。在Xcode中,您可以在目标下执行此操作 - &gt;构建阶段 - &gt;编译来源。
答案 1 :(得分:0)
您正在声明C ++类
class CAX4CCString {
在Objective-C类MyClass
内部。我担心Objective-C不支持这一点。
我认为您可以在全局范围内声明CAX4CCString
,然后从Objective-C类中实例化并使用它:
//.mm file
class CAX4CCString {
....
};
@implementation MyClass
@synthesize record, cancel, stop, play;
@synthesize playbackWasInterrupted;
@synthesize player, recorder;
...
<do something with CAX4CCString>
@end