iOS: define and use C++ in Objective-C++

落花浮王杯 提交于 2019-12-12 04:59:24

问题


Can anyone tell me on how to define and use BinaryWriter and BinaryReader (from OpenFrameworks project on GitHub) C++ classes in iOS 5.x -> Objective-C++ ?

what i do:

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

but in mm file i have compilation error:

No matching constructor for initialization of 'Poco::BinaryWriter'

What is wrong and whar to do?

p.s. path to Headers of OpenFrameworks is configured in setting of project and linker can see Poco classes.

Thanks you.


回答1:


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).




回答2:


You just have to set your .m to .mm and then you can use c++.

So, from

  • class.h
  • class.m

to

  • class.h
  • class.mm

This line

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

creates your poco::binarywriter. The error

No matching constructor for initialization of 'Poco::BinaryWriter'

Says that you are not creating it correctly.

You have to properly create it following these guidelines:

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.



回答3:


This is not an answer to your specific question but general advice.

With more recent versions of the LLVM compiler (i.e. Xcode 4.x) you can put your instance variables in the implementation, not the interface. i.e.

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

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

// etc

@end

This means that your instance variables are now hidden from files that import the header and the header now contains no C++ code so you can import it into other Objective-C files without making them Objective-C++



来源:https://stackoverflow.com/questions/10910408/ios-define-and-use-c-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!