0

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.

0

3 Answers 3

1

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.
2
  • updated question, i did it before you tell that, but no effect, compiler Error happens. Jun 6, 2012 at 8:50
  • 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
    – Pochi
    Jun 6, 2012 at 9:19
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).

4
  • this give me Error: "No member named 'cout' in namespace 'std'; did you mean 'count'?" Jun 6, 2012 at 12:54
  • right, you need to add #include <iostream> atop you .mm file. That's only to check if it's working though. You will want to use your own std::ostream later on. I'm updating my answer accordingly. Jun 6, 2012 at 13:21
  • you are real Snip3r :) worked, can you suggest me to create MemoryStream and use it with BinaryWriter? (iam in core C#.NET programmer and my experience with Objc is 1 month) Jun 6, 2012 at 15:10
  • Well MemoryStream is a .Net class, and I don't know the equivalent for C++ OpenFrameworks.. BUT you can use Monotouch to develop in C#.Net on iPhone xamarin.com/monotouch Jun 6, 2012 at 19:05
0

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++

1
  • sure, i know that, but i was thinking maybe error joined to Property declaration :) but anyway thanks Jun 6, 2012 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.