Estoy intentando mezclar Objective-C con C++. Cuando compilo el código, recibo varios errores.Mezcla de Objective-C y 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++";
}
};
Éstos son los errores:
/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)
No se olvide que obligar a los usuarios de su clase a ObjC use ObjC++ si coloca C++ en el encabezado; use punteros opacos para evitar eso. –
@gf Entonces, ¿cómo recomendarías hacer esto entonces? ¿Debería usar un puntero de vacío, o puedo usar un tipo de identificación? – LandonSchropp
Para evitar perder seguridad tipo yo personalmente prefiero las estructuras declaradas (http://stackoverflow.com/questions/2262011/adding-c-object-to-objective-c-class/2262395) pero también he visto * "privado" * Clases ObjC (es decir, a través de variables de instancia 'id'). –