iOS:在Objective-C ++中定义和使用C ++

任何人都可以告诉我如何定义和使用iOS 5.x中的BinaryWriter和BinaryReader ( 从GitHub上的OpenFrameworks项目 )C ++类 – > Objective-C的?

我做的事:

AppDelegate.h

#import <UIKit/UIKit.h> #import "Poco/BinaryWriter.h" @interface AppDelegate : UIResponder <UIApplicationDelegate>{ Poco::BinaryWriter *_myBinaryWriter; } @property (strong, nonatomic) UIWindow *window; @end 

AppDelegate.mm

 #import "AppDelegate.h" @implementation AppDelegate @synthesize window = _window; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; _myBinaryWriter = new Poco::BinaryWriter(NULL, NULL); [self.window makeKeyAndVisible]; return YES; } @end 

但在毫米文件我有编译错误:

没有匹配的构造函数来初始化'Poco :: BinaryWriter'

什么是错误的,要做什么?

ps到OpenFrameworks的头部的path是在项目的设置中configuration的,链接器可以看到Poco类。

谢谢。

Poco :: BinaryWriter(NULL,NULL)在BinaryWriter.h中没有具有该签名的构造函数,并且不能将NULL从NULL转换为std :: ostream&。

用标准输出(std :: cout)testingBinaryWriter:

 #import "AppDelegate.h" #include <iostream> @implementation AppDelegate @synthesize window = _window; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Only for testing _myBinaryWriter = new Poco::BinaryWriter(std::cout); (*_myBinaryWriter) << 1 << 3.14f; [self.window makeKeyAndVisible]; return YES; } @end 

一旦你确认这确实正在工作,你可以继续使用其他std :: ostream派生类,如std :: ofstream(输出文件stream)。

你只需要将.m设置为.mm,然后就可以使用c ++。

所以,从

  • class.h
  • class.m

  • class.h
  • class.mm

这条线

 _myBinaryWriter = new Poco::BinaryWriter(NULL, NULL); 

创build您的poco ::二进制。 错误

没有匹配的构造函数来初始化'Poco :: BinaryWriter'

说你没有正确创build它。

您必须按照以下准则正确创build它:

 BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); /// Creates the BinaryWriter. BinaryWriter(std::ostream& ostr, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); /// Creates the BinaryWriter using the given TextEncoding. /// /// Strings will be converted from the currently set global encoding /// (see Poco::TextEncoding::global()) to the specified encoding. 

这不是对你的具体问题的答案,而是一般的build议。

使用更新版本的LLVM编译器(即Xcode 4.x),可以将实例variables放在实现中,而不是接口。 即

 @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end @implementation AppDelegate { Poco::BinaryWriter *_myBinaryWriter; } // etc @end 

这意味着您的实例variables现在对导入标题的文件是隐藏的,并且标题现在不包含C ++代码,因此您可以将其导入到其他Objective-C文件中,而无需将其导入Objective-C ++