【问题标题】:Text-to-speech pass string to speak文本到语音传递字符串来说话
【发布时间】:2016-12-13 17:07:51
【问题描述】:

我是编程新手,所以需要您的帮助,只是尝试在 CLR 控制台应用程序中使用语音合成器对象在 Windows 10 上的 Visual Studio 2015 中使用 Text to Speech C++ 编写程序。但我想不通,如何通过变量“t”让 line 不仅能说话 synth->Speak("Line saved");和 synth->Speak("线路存在"); ,但用“t”这样:“行(文本行)存在”。那么如何将字符串传递给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;
}

和元帅这样:

#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;
}

我收到了这个错误:

错误 C4996 'msclr::interop::error_reporting_helper<_to_type>:‌​:marshal_as': 库或头文件不支持此转换 不包括此转换所需的。

错误 C2065 '_This_conversion_is_not_supported':未声明的标识符 X_TTS2 c:\program files (x86)\microsoft visual studio 14.0\vc\include\msclr\marshal.h 219

【问题讨论】:

    标签: c++ visual-studio-2015 text-to-speech sapi speech-synthesis


    【解决方案1】:

    根据文档:

    marshal_as

    如果您尝试编组一对不受支持的数据类型,marshal_as 将在编译时生成错误 C4996。阅读与此错误一起提供的消息以获取更多信息。 C4996 错误不仅可以针对已弃用的功能生成。一个例子是尝试编组一对不受支持的数据类型

    记录了支持的转换:

    Overview of Marshaling in C++

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

    #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.hmarshal_as() 支持封送const char*

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

    【讨论】:

    • _你好,谢谢你的反馈,非常有帮助,看来我可以这样使用它,工作#include &lt;msclr\marshal.h&gt; std::string line = "Line " + t + " exists!"; synth-&gt;Speak(marshal_as&lt;String^&gt;(line.c_str()));
    • @ztores 你可以,是的。 marshal_as &lt;String^&gt;(line) 也应该可以工作,因为 std::string->String^ 是受支持和记录在案的转换。
    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2019-05-13
    • 1970-01-01
    相关资源
    最近更新 更多