5

我正在编写一个 C++ WinRT 组件 DLL 用于我的基于 .NET 的 WinRT 应用程序。DLL 定义了一个 SoundSample 引用类,该类通过调用IXAudio2::CreateSourceVoice创建 XAudio 语音。CreateSourceVoice 采用“IXAudio2VoiceCallback *pCallback”参数来启用各种音频事件的回调。现在我正在尝试根据这篇文章实现该回调。XAudio 应该只是回调我的 SoundCallback 类的方法,定义为:

#pragma once
#include "xaudio2.h"
#include "pch.h"

class SoundCallback
    : public IXAudio2VoiceCallback
{
private:
    //SoundSample^ sample; //does not compile

public:
    SoundCallback(void);
    ~SoundCallback(void);

    //Called when the voice has just finished playing a contiguous audio stream.
    void OnStreamEnd();
    void OnVoiceProcessingPassEnd();
    void OnVoiceProcessingPassStart(UINT32 SamplesRequired);
    void OnBufferEnd(void * pBufferContext);
    void OnBufferStart(void * pBufferContext);
    void OnLoopEnd(void * pBufferContext);
    void OnVoiceError(void * pBufferContext, HRESULT Error);
};

一切都很好,直到我试图弄清楚如何从我的本机回调类的实例回调到父 SoundSample 对象。我在想我可以将 SoundSample 类的实例传递给 SoundCallback 对象,但似乎它不允许我在本机类中声明 ref 类字段:

SoundCallback.h(9): error C2143: syntax error : missing ';' before '^'
SoundCallback.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SoundCallback.h(9): error C3699: '^' : cannot use this indirection on type 'int'

我回顾了在本机 C++ 中实现回调,到目前为止我找不到合理的解决方案。最好/最简单的方法是什么?

4

1 回答 1

3

解决了它(感谢Jeremiah Morrill) - 问题不在于阻止在基本类中使用 ref 类的任何障碍。C4430 意味着 SoundSample 是一种无法识别的类型,它被 Intellisense 隐藏了——因为这似乎表明 SoundSample 是已知的。需要添加的是 SoundSample 类型的声明,这一切都开始正常工作。

我刚刚添加

namespace MyNamespace { ref class SoundSample; }

在 SoundCallback 类声明之前,然后 SoundCallback 类可以声明:

MyNamespace::SoundSample^ sample;
于 2012-04-03T22:10:08.223 回答