0

谁能告诉我如何在 iOS 5.x -> Objective-C++ 中定义和使用BinaryWriterBinaryReader来自 GitHub 上的 OpenFrameworks 项目)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

但在 mm 文件中我有编译错误:

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

什么是错的,怎么办?

在项目设置中配置了 OpenFrameworks 标头的 ps 路径,链接器可以看到 Poco 类。

谢谢。

4

3 回答 3

1

您只需将 .m 设置为 .mm 即可使用 c++。

所以,从

  • 类.h
  • 类.m

  • 类.h
  • 类.mm

这条线

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

创建你的 poco::binarywriter。错误

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

说您没有正确创建它。

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

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.
于 2012-06-06T08:11:39.853 回答
0

Poco::BinaryWriter(NULL, NULL) there are no constructors with that signature in BinaryWriter.h, and you cannot convert from NULL to std::ostream&.

To test the BinaryWriter with the standard output (std::cout):

#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

Once you confirm that this is indeed working you can move on to actually using other std::ostream derived classes such as std::ofstream (output file stream).

于 2012-06-06T09:19:42.830 回答
0

这不是对您具体问题的回答,而是一般性建议。

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

@implementation AppDelegate
{
    Poco::BinaryWriter *_myBinaryWriter;
}

// etc

@end

这意味着您的实例变量现在从导入标头的文件中隐藏,并且标头现在不包含 C++ 代码,因此您可以将其导入其他 Objective-C 文件而无需使其成为 Objective-C++

于 2012-06-06T09:29:26.897 回答