-1

我正在处理以下 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)
    {
    }

为什么我会收到这些错误?

4

3 回答 3

0

First, you need do Add reference to your project System::Speech and than use next namespaces:

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");

}

This is solution for the first button, now try your self to resolve next syntaxes. Good luck! :)

于 2014-06-03T21:28:02.733 回答
0

我认为它应该SpeechRecognizer^在您的功能开始时。不要忘记gcnew它。

于 2013-05-16T09:37:12.083 回答
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) aso

于 2013-05-16T09:47:25.413 回答