【问题标题】:Using Methods Without Declaring them in the header使用方法而不在标题中声明它们
【发布时间】:2011-09-09 11:12:22
【问题描述】:

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

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

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

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

其中很多内容都很新,所以我的术语可能有点不对劲。如果我能以任何方式澄清我的问题,请告诉我。

TLDR:X 类如何安全地调用 Y 类中的方法,而无需在 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

【问题讨论】:

    标签: objective-c oop


    【解决方案1】:

    我不太确定你的问题是什么,因为我找不到任何真正任何东西的东西。 (我什至Cmd+F'd for "?")。但是,我假设您是在问您可以做些什么来消除该警告?

    -如果在调用它的方法之上声明了一个方法,则不应出现警告。 (方法按顺序编译)。要摆脱这样的警告,您必须转发声明方法签名。 (这通常在标题中,但我认为没有什么能阻止你在 .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. 
    }
    

    【讨论】:

    • 这个前向声明是否允许从其他类调用该方法?
    • 除非它在标题中,否则它仍然会产生来自其他类的警告。我认为它会在运行时正常工作
    • 是的,它在运行时确实有效。不过,我只是对那里出现警告不满意。我也稍微澄清了我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多