【发布时间】: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