我最近開始閱讀關於alsa api的內容。我試圖寫用於打開缺省設備,並讀取像最大速率基本參數,信道數量等在C++類的成員函數中使用c庫變量/ struct成員
我的類的頭文件是一個C++類:
#include <alsa/asoundlib.h>
#include <iostream>
class AlsaParam{
snd_pcm_t* pcm_handle;
snd_pcm_hw_params_t* hw_param;
....
public:
int pcm_open();
.....
};
內部pcm_open()
int AlsaParam::pcm_open(){
int err = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if(err > -1)
std::cout << pcm_handle->name << std::endl; //Just to test if it works
return err;
}
我獲得以下錯誤:
error: invalid use of incomplete type ‘snd_pcm_t {aka struct _snd_pcm}’
std::cout << pcm_handle->name << std::endl;
^
In file included from /usr/include/alsa/asoundlib.h:54:0,
from alsa_param.h:4,
from alsa_param.cpp:1:
/usr/include/alsa/pcm.h:341:16: error: forward declaration of ‘snd_pcm_t {aka struct _snd_pcm}’
typedef struct _snd_pcm snd_pcm_t;
^
從這個錯誤我明白了asoundlib.h只對struct snd_pcm_t使用typedef,但是它在別處被定義。我對麼?有什麼辦法可以解決這個問題嗎?一般來說,如果我們在C++類中包含一些需要記住/避免的c庫函數?謝謝
snd_pcm_name(pcm_handle)的作品。正如你所說struct _snd_pcm的佈局可能會改變,它不會暴露在庫API中,並且其實現是隱藏的? – sap