我正在尝试按照Initialising PortAudio tutorial中的描述初始化portaudio。
它说要检查初始化过程中是否有错误,如下所示:
PaError err = Pa_Initialize();
if (err != paNoError) goto error;
这是我正在使用的确切代码。
我在OS X Mojave 10.14.4上运行它,使用Xcode 10.1和10.12 OS X SDK。
我试图找到PortAudio文档中的错误标签无效,并且名为error
的文件中没有变量。
到目前为止,完整的计划是:
# include <iostream>
# include "portaudio.h"
using namespace std;
// Typedef and demo callbacks here.
int main(int argc, const char * argv[])
{
PaError err = Pa_Initialize();
if (err != paNoError) goto error;
// Nothing here yet.
err = Pa_Terminate();
if (err != paNoError)
{
printf("Port audio error terminating: %s", Pa_GetErrorText(err));
}
return 0;
}
据我在教程中可以看出,这应该是一个有效的语句,但Xcode显示语法错误:Use of undeclared label 'error'
检查c++ reference for goto statements an example program for PortAudio,问题来自于假设goto
可以访问portaudio.h文件中定义的内容,但实际情况并非如此。
如果你遇到这个问题,我认为你也不熟悉goto
的陈述。
本教程假设主要功能的一部分专门用于解决错误。为了解决这个问题,我们需要在main
函数中定义一个错误标签,负责响应错误。
例如:
int main(void) {
PaError err;
// Checking for errors like in the question code, including goto statement.
return 1; // If everything above this goes well, we return success.
error: // Tells the program where to go in the goto statement.
Pa_Terminate(); // Stop port audio. Important!
fprintf( stderr, "We got an error: %s/n", Pa_GetErrorMessage(err));
return err;
}