title: Windows Speech API in Qt. Works in main(), but lots of errors when in class
body: <p>So I first tried to make a short example just to get text-to-speech working in main function. This worked and no problems. Code looks like this:</p>
  
  <p><strong>main.cpp:</strong></p>
  
  <pre><code>#include &lt;QtGui/QGuiApplication&gt;
  #include "qtquick2applicationviewer.h"
  #include &lt;QDebug&gt;
  #include &lt;sapi.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;atlbase.h&gt;
  #include "sphelper.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
  
      QtQuick2ApplicationViewer viewer;
      viewer.setMainQmlFile(QStringLiteral("qml/GC/main.qml"));
      viewer.showExpanded();
  
      CComPtr&lt;ISpObjectToken&gt;         cpVoiceToken;
      CComPtr&lt;IEnumSpObjectTokens&gt;    cpEnum;
      ISpVoice *                      pVoice = NULL;
      ULONG                           count = 0;
  
      if (FAILED(::CoInitialize(NULL)))
          return FALSE;
  
      HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&amp;pVoice);
  
      if(SUCCEEDED(hr))
      {
          //Enumerate voices.
          hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &amp;cpEnum);
      }
      else
      {
           qDebug() &lt;&lt; "Failed to initialize SAPI5";
      }
  
      if(SUCCEEDED(hr))
      {
          //Get number of voices.
          hr = cpEnum-&gt;GetCount(&amp;count);
          qDebug() &lt;&lt; "TTS voices found: " + QString::number(count);
      }
      else
      {
          qDebug() &lt;&lt; "Failed to enumerate voices. Using default.";
          hr = S_OK;
      }
  
      if(SUCCEEDED(hr))
      {
          cpVoiceToken.Release();
  
          cpEnum-&gt;Item(4, &amp;cpVoiceToken);
  
          pVoice-&gt;SetVoice(cpVoiceToken);
          hr = pVoice-&gt;Speak(L"Hello! How are you?", 0, NULL);
          pVoice-&gt;Release();
          pVoice = NULL;
      }
  
      ::CoUninitialize();
  
  
  
      qDebug() &lt;&lt; "End";
  
      return app.exec();
  }
  </code></pre>
  
  <p>This outputs the number of voices I have installed on my computer and says the text: "Hello! How are you?".</p>
  
  <p>When I now move this code to a class:</p>
  
  <p><strong>tts.h:</strong></p>
  
  <pre><code>#ifndef TTS_H
  #define TTS_H
  
  #include &lt;sapi.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;atlbase.h&gt;
  #include "sphelper.h"
  
  class Tts
  {
  public:
      Tts();
      bool Initialize();
      HRESULT Speak(char * text, ISpVoice * pVoice);
  
  private:
  
  };
  
  #endif // TTS_H
  </code></pre>
  
  <p><strong>tts.cpp:</strong></p>
  
  <pre><code>#include "tts.h"
  #include &lt;QDebug&gt;
  #include &lt;sapi.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;atlbase.h&gt;
  #include "sphelper.h"
  
  
  Tts::Tts()
  {
  }
  
  bool Tts::Initialize()
  {    
      CComPtr&lt;ISpObjectToken&gt;         cpVoiceToken;
      CComPtr&lt;IEnumSpObjectTokens&gt;    cpEnum;
      ISpVoice *                      pVoice = NULL;
      ULONG                           count = 0;
  
      if (FAILED(::CoInitialize(NULL)))
          return false;
  
      HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&amp;pVoice);
  
      if(SUCCEEDED(hr))
      {
          //Enumerate voices.
          hr = SpEnumTokens(SPCAT_VOICES, NULL, NULL, &amp;cpEnum);
      }
      else
      {
          qDebug() &lt;&lt; "Failed to initialize SAPI5";
      }
  
      if(SUCCEEDED(hr))
      {
          //Get number of voices.
          hr = cpEnum-&gt;GetCount(&amp;count);
          qDebug() &lt;&lt; "TTS voices found: " + QString::number(count);
      }
      else
      {
          qDebug() &lt;&lt; "Failed to enumerate voices. Using default.";
          hr = S_OK;
      }
  
      if(SUCCEEDED(hr))
      {
          cpVoiceToken.Release();
  
          cpEnum-&gt;Item(4, &amp;cpVoiceToken);
  
          pVoice-&gt;SetVoice(cpVoiceToken);
          Speak("Some text here", pVoice);
          pVoice-&gt;Release();
          pVoice = NULL;
      }
  
      ::CoUninitialize();
  
      return true;
  }
  
  HRESULT Tts::Speak(char * text, ISpVoice * pVoice)
  {
      HRESULT hr;
  
      hr = pVoice-&gt;Speak(L"Hello! How are you?", 0, NULL);
  
      return hr;
  }
  </code></pre>
  
  <p>New <strong>main.cpp:</strong></p>
  
  <pre><code>#include &lt;QtCore&gt;
  #include &lt;QtGui/QGuiApplication&gt;
  #include &lt;QtQuick&gt;
  #include "qtquick2applicationviewer.h"
  #include &lt;QDebug&gt;
  #include "tts.h"
  
  int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
      QtQuick2ApplicationViewer viewer;
      viewer.setMainQmlFile(QStringLiteral("qml/GC/main.qml"));
  
      viewer.showExpanded();
      viewer.showMaximized();
  
      return app.exec();
  }
  </code></pre>
  
  <p>I have just included the tts.h header file and get a lot of errors. I have tried rebuilding, cleaning and deleting the files in the build folder, but no luck. When building without the tts.h header file included in main the program runs as normal.</p>
  
  <p>I don't understand why this happens when I move it to a class file. Here is the .pro file and errors:</p>
  
  <p><strong>.pro:</strong></p>
  
  <pre><code># Add more folders to ship with the application, here
  folder_01.source = qml/GC
  folder_01.target = qml
  DEPLOYMENTFOLDERS = folder_01
  
  # Additional import path used to resolve QML modules in Creator's code model
  QML_IMPORT_PATH =
  
  # If your application uses the Qt Mobility libraries, uncomment the following
  # lines and add the respective components to the MOBILITY variable.
  # CONFIG += mobility
  # MOBILITY +=
  
  # The .cpp file which was generated for your project. Feel free to hack it.
  SOURCES += main.cpp \
      simkeyevent.cpp \
      serialthread.cpp \
      serial.cpp \
      tts.cpp
  
  # Installation path
  # target.path =
  
  # Please do not modify the following two lines. Required for deployment.
  include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
  qtcAddDeployment()
  
  #Manually added:
  QT += core gui serialport
  
  HEADERS += \
      simkeyevent.h \
      serialthread.h \
      serial.h \
      tts.h
  </code></pre>
  
  <p><strong>Errors:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/yZ3ES.png" alt="First errors"></p>