我是编程新手,所以需要你的帮助,只是尝试在 CLR 控制台应用程序中使用语音合成器对象在 Windows 10 上的 Visual Studio 2015 中使用文本到语音 C++ 编写程序。但我想不通,如何通过变量“t”获取线路来说话,而不仅仅是 synth->Speak("Line saved");和 synth->Speak("Line exist"); ,但是用这样的“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;
}
用编码(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
最佳答案
根据文档:
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
记录支持的转换:
如果您使用 marshal_cppstd,
,您的示例执行的操作:marshal_as()
函数支持将 std::string
编码为 System::String^
.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.h
,marshal_as()
支持编码(marshal) const char*
:
#include <msclr\marshal.h>
std::string line = "Line " + t + " exists!";
synth->Speak(marshal_as<String^>(line.c_str()));
关于c++ - 文字转语音传字符串说话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38820483/