2016-05-24 5 views
0

私はObjective Cのクラスでこの方法をカスタムC++クラスを統合しようとしています:の初期化に該当するコンストラクタ - C++&Objective Cの

C++クラスのヘッダー

class Analyzer 
{ 

public: 

    Analyzer(std::string const& face_cascade_url, std::string const& eyes_cascade_url, std::string const& nose_cascade_url); 

}; 

のObjective Cヘッダー:

@interface cvVideoWrapper : UIViewController <CvVideoCameraDelegate> 

@property Analyzer analyzer; 

@end 

のObjective Cの実装:

@implementation cvVideoWrapper 


-(void) get_ready { 

    NSString* face_filename = [[NSBundle mainBundle] 
           pathForResource:@"haarcascade_frontalface_alt2" 
           ofType:@"xml"]; 
    NSString* eyes_filename = [[NSBundle mainBundle] 
           pathForResource:@"haarcascade_eye_tree_eyeglasses" 
           ofType:@"xml"]; 
    NSString* nose_filename = [[NSBundle mainBundle] 
           pathForResource:@"haarcascade_mcs_nose" 
           ofType:@"xml"]; 

    self.analyzer = Analyzer([face_filename UTF8String], [eyes_filename UTF8String], [nose_filename UTF8String]); 
} 

@end 

私はObjective Cの実装では、このエラーを取得しています:

No matching constructor for initialization of 'Analyzer' 

どのように私はこの問題を解決することができますか?

答えて

1

ここで前方宣言を使用してヘッダーをサニタイズすることをお勧めします。インスタンスの代わりにポインタを使用します。

のObjective Cヘッダー:

class Analyzer; 

@interface cvVideoWrapper : UIViewController <CvVideoCameraDelegate> 

@property Analyzer* analyzer; 
//    ^^ it is a pointer now 

@end 

そして、Objective Cの実装で:

@implementation cvVideoWrapper 

-(void) get_ready { 

    NSString* face_filename = [[NSBundle mainBundle] 
           pathForResource:@"haarcascade_frontalface_alt2" 
           ofType:@"xml"]; 
    NSString* eyes_filename = [[NSBundle mainBundle] 
           pathForResource:@"haarcascade_eye_tree_eyeglasses" 
           ofType:@"xml"]; 
    NSString* nose_filename = [[NSBundle mainBundle] 
           pathForResource:@"haarcascade_mcs_nose" 
           ofType:@"xml"]; 

    // Create object here with new: 
    self.analyzer = new Analyzer([face_filename UTF8String], [eyes_filename UTF8String], [nose_filename UTF8String]); 
} 

// Don't forget to cleanup when you're done: 
- (void)dealloc 
{ 
    delete self.analyzer; 
    [super dealloc]; 
} 

@end 
0

私はC++でにObjCを結びつける方法がわからないが、問題があるように、それは私には見えます値によってAnalyzerを格納していますが、デフォルトコンストラクタはありません。 C++では、コンストラクタの初期化子リストにコンストラクタの引数を指定して、そのクラスを含むクラスのコンストラクタを指定する必要があります。それができない場合は、ポインタで保存する必要があります。

また、Analyzerには代入演算子も含まれていないように見えるため、問題になります。

1

アナライザーのヘッダーが含まれており、Objective-C実装がObjective-C++としてコンパイルされていると仮定すると、UTF8Stringによって返された値をstd::stringに変換します。コンパイラーは、正しいシグニチャーを持つコンストラクターも、UTF8String戻り値のタイプの自動変換可能な値も見つかりません。

std::string([face_filename UTF8String])などが動作するはずです。

関連する問題