使用方法而不在标题中声明它们

时间:2011-09-09 11:12:22

标签: objective-c oop

最近我在代码中使用了一个不透明的指针类型。我这样做是因为我想在我的obj c项目中使用c ++代码,而不必将每个文件都更改为.mm。

我使用c ++代码的方式是我有一个指向我的c ++代码的不透明指针作为.mm文件的成员。所有的c ++都隐藏在实现文件中。

在这个包含我的c ++的类中,我需要导入一个现有的类“MyClass”。我可以在实现类中导入它,但如果我尝试在头文件中导入它,我会得到c ++错误,说“ISO C ++禁止声明'CARingBufferCPPWrapper'没有类型”。

我“可以”只在.mm文件中编写方法并从标题中省略它但我收到警告说我的.mm文件可能不响应该方法。

很多这个都是新的,所以我的术语可能有些偏差。如果我能以任何方式澄清我的问题,请告诉我。

TLDR:如果没有在Y类标题中声明方法,Class X如何安全地调用Y类中的方法?

//我的标题

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#define kBufferLength 5120

//#define "Myclass.h"
typedef struct ringbufferobj * RingBufferOBJ;


RingBufferOBJ newRingBufferOBJ();






@interface CARingBufferCPPWrapper : NSObject {

    RingBufferOBJ ringbuffer;
    NSThread  *producerthread;
    int duration;

}
@property (nonatomic, retain) NSThread *producerthread;
@property(nonatomic)int duration;

//-(void)myclassfunction(MyClass *)classref

@end

//我的实施.mm

#import "CARingBufferCPPWrapper.h"
#import "CARingBuffer.h"
#import <AudioToolbox/AudioToolbox.h>
#import "MyClass.h"

struct ringbufferobj
{
    CARingBuffer *ringbuffer;
    AudioBufferList *inputbuffer; 

    Float64 firstInputSampleTime;
    Float64 firstOutputSampleTime;
    Float64 inToOutSampleTimeOffset;





    BOOL producerthreadisrunning;
};

RingBufferOBJ newRingBufferOBJ(){
    RingBufferOBJ ringbuffer=(RingBufferOBJ)malloc(sizeof(struct ringbufferobj));

    return ringbuffer;
}


@implementation CARingBufferCPPWrapper
@synthesize producerthread;
@synthesize duration;


-(void)myclassfunction(MyClass *)classref
{

}
@end

1 个答案:

答案 0 :(得分:0)

我不太确定你的问题是什么,因为我找不到任何实际询问的东西。 (我甚至Cmd+F代表“?”)。不过,我想你会问你能做些什么来摆脱那个警告?

- 如果方法在方法之上声明它被称为它,则应该没有警告。 (方法按顺序编译)。要摆脱这样的警告,你必须转发声明方法签名。 (这通常在标题中,但我认为没有什么能阻止你在你的.m顶部做这件事)

-(void) methodA {
    ..do something
    [self methodB]; //Warning here because the compiler has not yet seen methodB

}

-(void) method B {
    ..[self methodA]; //No warning, compiler knows what methodA is at this point. 
}