【问题标题】:iOS: define and use C++ in Objective-C++iOS:在 Objective-C++ 中定义和使用 C++
【发布时间】:2012-06-06 08:07:43
【问题描述】:

谁能告诉我如何在 iOS 5.x -> Objective-C++ 中定义和使用BinaryWriterBinaryReader (from OpenFrameworks project on GitHub) 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 的 Headers 路径在项目设置中配置,链接器可以看到 Poco 类。

谢谢。

【问题讨论】:

    标签: c++ objective-c ios poco-libraries


    【解决方案1】:

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

    所以,从

    • class.h
    • class.m

    • class.h
    • class.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.
    

    【讨论】:

    • 更新的问题,我在你说之前就做了,但是没有效果,编译器错误发生了。
    • 这一行 _myBinaryWriter = new Poco::BinaryWriter(NULL, NULL);创建你的 poco::binarywriter。 'Poco::BinaryWriter' 的初始化错误没有匹配的构造函数表示您没有正确创建它
    【解决方案2】:

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

    使用标准输出 (std::cout) 测试 BinaryWriter:

    #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(输出文件流)。

    【讨论】:

    • 这给了我错误:“命名空间 'std' 中没有名为 'cout' 的成员;您的意思是 'count'?”
    • 对,您需要在 .mm 文件顶部添加#include 。这只是为了检查它是否有效。稍后您将需要使用自己的 std::ostream。我正在相应地更新我的答案。
    • 你是真正的 Snip3r :) 工作,你能建议我创建 MemoryStream 并将其与 BinaryWriter 一起使用吗? (我是核心 C#.NET 程序员,我使用 Objc 的经验是 1 个月)
    • 嗯,MemoryStream 是一个 .Net 类,我不知道 C++ OpenFrameworks 的等价物。但是你可以使用 Monotouch 在 iPhone 上的 C#.Net 中开发 xamarin.com/monotouch
    【解决方案3】:

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

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

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

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

    【讨论】:

    • 当然,我知道,但我想也许错误加入了属性声明:) 但无论如何谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多