Смешивание Objective C и C++

Я пытаюсь смешать Objective C с C++. Когда я компилирую код, я получаю несколько ошибок.

A.h

#import <Cocoa/Cocoa.h>
#include "B.h"

@interface A : NSView {
    B *b;
}

-(void) setB: (B *) theB;

@end

A.m

#import "A.h"

@implementation A

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
}

-(void) setB: (B *) theB {
    b = theB;
}

@end

B.h

#include <iostream>

class B {

    B() {
        std::cout << "Hello from C++";
    }

};

Вот ошибки:

/Users/helixed/Desktop/Example/B.h:1:0 /Users/helixed/Desktop/Example/B.h:1:20: error: iostream: No such file or directory
/Users/helixed/Desktop/Example/B.h:3:0 /Users/helixed/Desktop/Example/B.h:3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'B'
/Users/helixed/Desktop/Example/A.h:5:0 /Users/helixed/Desktop/Example/A.h:5: error: expected specifier-qualifier-list before 'B'
/Users/helixed/Desktop/Example/A.h:8:0 /Users/helixed/Desktop/Example/A.h:8: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:26:0 /Users/helixed/Desktop/Example/A.m:26: error: expected ')' before 'B'
/Users/helixed/Desktop/Example/A.m:27:0 /Users/helixed/Desktop/Example/A.m:27: error: 'b' undeclared (first use in this function)
44
задан LandonSchropp 11 August 2013 в 22:30
поделиться

2 ответа

Вам нужно назвать файлы .m .mm . И вы сможете компилировать код C ++ с помощью Objective-C.

Итак, следуя вашему примеру, ваш файл AView.m должен называться AView.mm . Все просто. Работает очень хорошо. Я использую много контейнеров std (std :: vector, std :: queue и т. Д.) И устаревший код C ++ в проектах iPhone без каких-либо осложнений.

77
ответ дан 26 November 2019 в 22:03
поделиться

Ничего, я чувствую себя глупо. Все, что вам нужно сделать, это переименовать AView.m в AView.mm, чтобы компилятор знал, что это Objective-C ++, и компилировался без проблем.

5
ответ дан 26 November 2019 в 22:03
поделиться
Другие вопросы по тегам:

Похожие вопросы: