文字转语音传字符串说话

Text-to-speech pass string to speak

我是编程新手,所以需要你的帮助,只是想在 Visual Studio 2015 年 Windows 10 年在 CLR 控制台应用程序中使用语音合成器对象使用 Text to Speech C++ 编写程序.但我想不通,如何通过变量 "t" 来说话,而不仅仅是 synth->Speak("Line saved");和 synth->Speak("Line exist"); ,但 "t" 是这样的:"Line (text line) exist"。那么如何将字符串传递给 Speak 函数呢? 你能帮我弄清楚吗:

#include "stdafx.h"
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;
using namespace System;
using namespace System::Speech::Synthesis;
using namespace System::IO;

const string FILE_NAME = "lines.txt";

vector<string> getFileLines(string file) {
    ifstream in(FILE_NAME);
    vector<string> lines;
    for (string line; getline(in, line); ) {
        lines.push_back(line);
    }
    return lines;
}

string getUserInput() {
    string str;
    getline(cin, str);
    return str;
}

int main() 

{

    vector<string> lines = getFileLines(FILE_NAME);

    ofstream fileOut(FILE_NAME, ios::app);

    for (int n = 0; n < 10; n++)

    {

    cout << "Write: > ";
    std::string t = getUserInput();

    auto it = std::find(lines.begin(), lines.end(), t);

    if (it == lines.end()) {

        fileOut << t << endl;
        lines.push_back(t);

        cout << "Line \"" << t << "\" saved.\n";

        SpeechSynthesizer^ synth = gcnew SpeechSynthesizer();
        synth->Speak("Text saved");

    }

    else 

    {

        cout << "LIne \"" << t << "\" exist.\n";

        SpeechSynthesizer^ synth = gcnew SpeechSynthesizer();
        synth->Speak("Line exist");


    }
} 

    cout << endl;
    getUserInput();
    return 0;
}

这样与 marshal:

#include "stdafx.h"
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <msclr\marshal_cppstd.h>

using namespace msclr::interop;  
using namespace std;
using namespace System;
using namespace System::Speech::Synthesis;
using namespace System::IO;

const string FILE_NAME = "lines.txt";

vector<string> getFileLines(string file) {
    ifstream in(FILE_NAME);
    vector<string> lines;
    for (string line; getline(in, line); ) {
        lines.push_back(line);
    }
    return lines;
}

string getUserInput() {
    string str;
    getline(cin, str);
    return str;
}

int main() 

{

    vector<string> lines = getFileLines(FILE_NAME);

    ofstream fileOut(FILE_NAME, ios::app);

    for (int n = 0; n < 10; n++)

    {

    cout << "Write: > ";
    std::string t = getUserInput();

    auto it = std::find(lines.begin(), lines.end(), t);

    if (it == lines.end()) {

        fileOut << t << endl;
        lines.push_back(t);

        cout << "Line \"" << t << "\" saved.\n";

        String^ str = marshal_as<String^>(str);
        std::string line = "Line " + t + " exists!"; 
        synth->Speak(marshal_as<String^>(line));

    }

    else 

    {

        cout << "LIne \"" << t << "\" exist.\n";

        String^ str = marshal_as<String^>(str);
        std::string line = "Line " + t + " exists!"; 
        synth->Speak(marshal_as<String^>(line));

    }
} 

    cout << endl;
    getUserInput();
    return 0;
}

我遇到了这个错误:

Error C4996 'msclr::interop::error_reporting_helper<_To_Type,_From_Type,false>:‌​:marshal_as': This conversion is not supported by the library or the header file needed for this conversion is not included.

Error C2065 '_This_conversion_is_not_supported': undeclared identifier X_TTS2 c:\program files (x86)\microsoft visual studio 14.0\vc\include\msclr\marshal.h 219

根据文档:

marshal_as

If you try to marshal a pair of data types that are not supported, marshal_as will generate an error C4996 at compile time. Read the message supplied with this error for more information. The C4996 error can be generated for more than just deprecated functions. One example of this is trying to marshal a pair of data types that are not supported

记录支持的转换:

Overview of Marshaling in C++

如果您使用 marshal_cppstd.hmarshal_as() 函数支持将 std::string 编组为 System::String^,您的示例就是这样:

#include <msclr\marshal_cppstd.h>

std::string line = "Line " + t + " exists!"; 
synth->Speak(marshal_as<String^>(line));

所以你显示的错误没有意义,除非它指的是这个语句:

String^ str = marshal_as<String^>(str);

您正在尝试将 String^ 编组为 String^,这不是受支持的编组转换。此外,在声明它的同一语句中使用变量无论如何都是未定义的行为,因此您需要完全删除该语句,因为它没有用。

或者,如果您使用 marshal.h

marshal_as() 支持封送 const char*
#include <msclr\marshal.h>

std::string line = "Line " + t + " exists!";
synth->Speak(marshal_as<String^>(line.c_str()));