Trouble using opaque pointers in Objective C++

橙三吉。 提交于 2019-12-02 15:23:48

问题


The answer to this quesion explains that opaque pointers are a good way to include C++ member variables in an Objective C++ header. I'm getting compile errors when trying to follow the example. Here's the relevant code from my header, with the corresponding compiler errors shown as comments:

struct ADSR_opaque; // error: forward declaration of 'struct ADSR_opaque'

@interface LoopyPulser : NSObject{

    float _pulseRate;
    UInt32 tickInterval;
    UInt32 step;
    InMemoryAudioFile * audioFilePlayer;
    ADSR_opaque* env; //  error: expected specifier-qualifier-list before 'ADSR_opaque'
    Pattern * pattern;
    float loopLengthRatio;
    float volume;
}

Is there something simple I'm doing wrong here?


回答1:


I don't have any problem with the following minimal sample:

struct ADSR_opaque;
@interface LoopyPulser : NSObject {
    struct ADSR_opaque* env;
}
@end

If you include the header in plain Objective-C files (not Objective-C++), you have to add struct.

Alternatively use typedefs:

struct ADSR_opaque_;
typedef struct ADSR_opaque_ ADSR_opaque;
@interface LoopyPulser : NSObject {
    ADSR_opaque* env;
    // ...



回答2:


Does your source file actually have a .mm extension?



来源:https://stackoverflow.com/questions/2463970/trouble-using-opaque-pointers-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!