我有一个程序,要求一个人将他们想要翻译成Al Bhed的文本转换成are,这只是一个密码,字母在其中移动,并让SAPI说出来。字符串翻译得很好,但是下面这段代码:
hr = pVoice->Speak(sTranslated, 0, NULL);将不起作用,因为它显示“没有合适的从'std:: string‘到'const WCHAR*’的转换函数。我所需要的就是让语音说出翻译后的字符串。我该怎么做呢?”
发布于 2014-10-01 10:24:23
首先,您需要将使用char类型的std::string内容转换为使用wchar_t类型的std::wstring。这是因为ISpVoice::Speak()函数要求第一个参数的类型为LPCWSTR,即“指向宽字符串的常量指针”。以下功能可能会对您有所帮助。
inline std::wstring s2w(const std::string &s, const std::locale &loc = std::locale())
{
typedef std::ctype<wchar_t> wchar_facet;
std::wstring return_value;
if (s.empty())
{
return return_value;
}
if (std::has_facet<wchar_facet>(loc))
{
std::vector<wchar_t> to(s.size() + 2, 0);
std::vector<wchar_t>::pointer toPtr = &to[0];
const wchar_facet &facet = std::use_facet<wchar_facet>(loc);
if (0 != facet.widen(s.c_str(), s.c_str() + s.size(), toPtr))
{
return_value = to.data();
}
}
return return_value;
}然后将代码行更改为以下代码行。
hr = pVoice->Speak(s2w(sTranslated).c_str(), 0, NULL);c_str()方法返回一个指向std::wstring对象的“C字符串等效项”的指针。它返回一个指向以null结尾的宽字符串的指针。
https://stackoverflow.com/questions/26132995
复制相似问题