由于转换问题,无法在c++ /CLI中执行语音识别

本文关键字:c++ CLI 语音识别 执行 于转换 转换 问题 | 更新日期: 2023-09-27 18:13:46

我正在编写以下c++/cli代码:

 #pragma once
namespace SpeechTest {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Speech::Synthesis;
    using namespace System::Speech::Recognition;
    using namespace System::Speech::AudioFormat;
    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }
    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::RichTextBox^  richTextBox1;
    private: System::Windows::Forms::Button^  button3;
    protected: 
    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(31, 28);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 0;
            this->button1->Text = L"speech";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // button2
            // 
            this->button2->Location = System::Drawing::Point(112, 28);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(75, 23);
            this->button2->TabIndex = 1;
            this->button2->Text = L"text";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
            // 
            // richTextBox1
            // 
            this->richTextBox1->Location = System::Drawing::Point(31, 92);
            this->richTextBox1->Name = L"richTextBox1";
            this->richTextBox1->Size = System::Drawing::Size(230, 120);
            this->richTextBox1->TabIndex = 2;
            this->richTextBox1->Text = L"";
            // 
            // button3
            // 
            this->button3->Location = System::Drawing::Point(197, 28);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(75, 23);
            this->button3->TabIndex = 3;
            this->button3->Text = L"button3";
            this->button3->UseVisualStyleBackColor = true;
            this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->richTextBox1);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
        }
#pragma endregion
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 SpeechSynthesizer synth;
                 synth.Rate = -2;
                 synth.Volume = 100;
                 synth.Speak("what is happening right now?");
             }
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e)
             {
                 SpeechRecognizer sr;
                 array<String>^ words = gcnew array<String>(3);
                 words[0] = "red";
                 words[1] = "green";
                 words[2] = "red";
                 Choices colors;
                 colors.Add(words);
                 GrammarBuilder gb;
                 gb.Append(colors);
                 Grammar g(gb);
                 sr.LoadGrammar(g);
                 System::IntPtr ptr = gcnew System::IntPtr(&sr_SpeechRecognized);
                 sr.SpeechRecognized += gcnew System::EventHandler(this,ptr);
             }
             void sr_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs^ e)
             {
             }
};
}

我张贴了这整个代码,以便读者可以知道行号,以便提供帮助。当我运行这段c++/CLI代码时,我得到了很多我无法解决的错误:

1>------ Build started: Project: SpeechTest, Configuration: Debug Win32 ------
1>  SpeechTest.cpp
1>c:'users'yohan'documents'visual studio 2010'projects'speechtest'speechtest'Form1.h(132): error C3149: 'System::String' : cannot use this type here without a top-level '^'
1>c:'users'yohan'documents'visual studio 2010'projects'speechtest'speechtest'Form1.h(132): error C3149: 'System::String' : cannot use this type here without a top-level '^'
1>c:'users'yohan'documents'visual studio 2010'projects'speechtest'speechtest'Form1.h(141): error C2664: 'void System::Speech::Recognition::GrammarBuilder::Append(System::String ^)' : cannot convert parameter 1 from 'System::Speech::Recognition::Choices' to 'System::String ^'
1>          No user-defined-conversion operator available, or
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:'users'yohan'documents'visual studio 2010'projects'speechtest'speechtest'Form1.h(143): error C2664: 'System::Speech::Recognition::Grammar::Grammar(System::String ^)' : cannot convert parameter 1 from 'System::Speech::Recognition::GrammarBuilder' to 'System::String ^'
1>          No user-defined-conversion operator available, or
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:'users'yohan'documents'visual studio 2010'projects'speechtest'speechtest'Form1.h(145): error C2664: 'System::Speech::Recognition::SpeechRecognizer::LoadGrammar' : cannot convert parameter 1 from 'System::Speech::Recognition::Grammar' to 'System::Speech::Recognition::Grammar ^'
1>          No user-defined-conversion operator available, or
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:'users'yohan'documents'visual studio 2010'projects'speechtest'speechtest'Form1.h(147): error C2276: '&' : illegal operation on bound member function expression
1>c:'users'yohan'documents'visual studio 2010'projects'speechtest'speechtest'Form1.h(147): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

所有这些错误都引用了下面的代码片段,它是从上面的

中提取出来的
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e)
 {
    SpeechRecognizer sr;
    array<String>^ words = gcnew array<String>(3);
    words[0] = "red";
    words[1] = "green";
    words[2] = "red";
    Choices colors;
    colors.Add(words);
    GrammarBuilder gb;
    gb.Append(colors);
    Grammar g(gb);
    sr.LoadGrammar(g);
    System::IntPtr ptr = gcnew System::IntPtr(&sr_SpeechRecognized);
    sr.SpeechRecognized += gcnew System::EventHandler(this,ptr);
}
void sr_SpeechRecognized(System::Object ^sender, SpeechRecognizedEventArgs^ e)
    {
    }

为什么我得到这些错误?

由于转换问题,无法在c++ /CLI中执行语音识别

我认为它应该是一个SpeechRecognizer^在你的功能开始。别忘了加gcnew

不知道Choices这样的类型是很难的。但是,words数组应该这样使用:

array<String^>^ words = gcnew array<String^>(3);
words[0] = "red";
words[1] = "green";
words[2] = "red";

此外,GrammarBuilder。Append似乎期望String^,但您使用Choices调用它。类似地,您需要修复Grammar构造函数的使用(需要String^而不是GrammarBuilder)。

首先,您需要添加引用到您的项目System::Speech,然后使用下一个名称空间:

using namespace System::Speech::Synthesis;
using namespace System::Speech::Recognition;
using namespace System::Speech::AudioFormat; 
//you put a C# code instead of C++/Cli, try this:
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    SpeechSynthesizer^ synth=gcnew SpeechSynthesizer();
    synth->Rate = -2;
    synth->Volume = 100;
    synth->Speak("She say: This is real C++/Cli");
}

这是第一个按钮的解决方案,现在尝试自己解决下一个语法。好运!:)