这是我的代码:

#include <sapi.h>

int main(int argc, char* argv[])
{
    ISpVoice * pVoice = NULL;

    if (FAILED(::CoInitialize(NULL)))
        return FALSE;

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice;);
    if( SUCCEEDED( hr ) )
    {
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    }

    ::CoUninitialize();
    return TRUE;
}

这是我的第一个SAPI 5.1 Run,这是MS提供的示例hello world。

我已经将SAPI“bin”位置添加到系统变量“Path”中。

以下是我的QT专案(.pro内容)
#-------------------------------------------------
#
# Project created by QtCreator 2013-04-26T12:59:05
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = Tired
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    ContentDetector.cpp \
    Speech.cpp

INCLUDEPATH += C:/opencv/build/include
INCLUDEPATH += C:/opencv/build/include/opencv
INCLUDEPATH += "C:/Program Files/Java/jdk1.7.0/include"
INCLUDEPATH += "C:/Program Files/Java/jdk1.7.0/include/win32"
INCLUDEPATH += "C:/Program Files/Microsoft Speech SDK 5.1/Include"

LIBS += C:/opencv/build/x86/vc9/lib/opencv_calib3d240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_contrib240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_core240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_features2d240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_flann240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_highgui240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_imgproc240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_objdetect240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_video240.lib
LIBS += "C:/Program Files/Java/jdk1.7.0/lib/jvm.lib"
LIBS += "C:/Program Files/Java/jdk1.7.0/lib/jawt.lib"
LIBS += "C:/Program Files/Microsoft Speech SDK 5.1/Lib/i386/sapi.lib"

请注意,SAPI包含在最后一个包含中,SAPI库是最后一个库。

当我运行此代码时,将产生跟随错误。
C:\Users\yohan\Documents\QTPeojects\Tired\Speech.cpp:10: error: C2143: syntax error : missing ')' before ';'
C:\Users\yohan\Documents\QTPeojects\Tired\Speech.cpp:10: error: C2059: syntax error : ')'

我不明白为什么。

更新资料

按照约翰的指示,以上错误现在消失了。但是,现在生成了以下错误。这是为什么?
Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoUninitialize@0 referenced in function _main

Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoCreateInstance@20 referenced in function _main

Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoInitialize@4 referenced in function _main

release\Tired.exe:-1: error: LNK1120: 3 unresolved externals

最佳答案

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice;);

应该
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);

您的版本中还有一个分号。

10-05 22:13