在QT中运行SAPI时会生成错误

时间:2013-05-03 07:07:12

标签: c++ windows qt text-to-speech sapi

请查看以下代码

#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提供的样本问候世界。

我已将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 lib是最后一个包。

当我运行此代码时,会生成以下错误。

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

4 个答案:

答案 0 :(得分:1)

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

应该是

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

你的版本中有一个额外的分号。

答案 1 :(得分:0)

缺少的函数位于 user32.lib 中(但不知道如何将其添加到qt proj中)

答案 2 :(得分:0)

您的链接器问题源于您没有在Qt .pro文件中正确引用库。

要添加库,您需要执行以下操作: -

LIBS + = -LC:/ opencv / build / x86 / vc9 / lib / -lopencv_calib3d240

请注意路径前面的-L和库名称前面的-l。

答案 3 :(得分:0)

唯一的方法是添加ATL。

  #define _ATL_APARTMENT_THREADED

#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override something,
//but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

#include <sapi.h>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    cout << "Hello" << endl;
    ISpVoice * pVoice = NULL;

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

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

    ::CoUninitialize();
    return TRUE;
}