私は、Objective-C の使い方を学び、Objective-C クラスを C++ にラップする方法についていくつかのチュートリアルを読もうとしています。すべてがエラーなしでコンパイルされる次のポイントに到達しましたが、プログラムを実行すると「セグメンテーション違反」が発生します。
わかりました、次のスニペットがあり、それをコンパイルするとしましょう: g++ -Wall -pedantic -framework Cocoa -x objective-c++ -o test test.mm
. どこが間違っていますか?
テスト.mm
#include <iostream>
#import "test-osx.m"
struct OpenControllerImpl
{
OpenController* wrapped;
};
class Panel {
OpenControllerImpl* impl;
public:
Panel() :
impl(new OpenControllerImpl) {
impl->wrapped = [[OpenController alloc] init];
}
~Panel() {
[(OpenController*)impl release];
}
void open() {
[(OpenController*)impl doOpen:impl->wrapped];
}
};
int main() {
Panel* openPanel = new Panel();
openPanel->open();
return 0;
}
テスト-osx.h
#import <Cocoa/Cocoa.h>
@interface OpenController : NSObject {
}
- (void)doOpen:(id)sender;
@end
テスト-osx.m
#import "test-osx.h"
#include <stdio.h>
@implementation OpenController
- (void)doOpen:(id)sender {
printf("here");
}
@end