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

问题描述 投票:-1回答:3

我正在研究以下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# visual-studio-2010 visual-c++ c++-cli speech-recognition
3个回答
0
投票

我认为您应该在函数开始时将其设为SpeechRecognizer^。不要忘记gcnew


0
投票

[不知道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)a.s.o。


0
投票

首先,您需要添加对项目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");

}

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

© www.soinside.com 2019 - 2024. All rights reserved.