在Objective C ++中使用不透明指针时遇到麻烦 [英] Trouble using opaque pointers in Objective C++

查看:123
本文介绍了在Objective C ++中使用不透明指针时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的答案解释了不透明的指针是在Objective C ++标头中包含C ++成员变量的好方法。我试图按照这个例子时遇到编译错误。这是我标题中的相关代码,相应的编译器错误显示为注释:

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?

推荐答案

我对以下最小样本没有任何问题:

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

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

如果在标准Objective-C中包含标题文件(不是Objective-C ++),你必须添加 struct

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

或者使用typedef:

Alternatively use typedefs:

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

这篇关于在Objective C ++中使用不透明指针时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆