![](/img/trans.png)
[英]no suitable constructor exists to convert from “const char [8]” to “std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>”
[英]Cannot convert from 'TCHAR [260]' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
错误C2440:“正在初始化”:无法从“ TCHAR [260]”转换为“ std :: basic_string <wchar_t,std :: char_traits <wchar_t>,std :: allocator <wchar_t >>
我收到此错误,在此问题上停留了几个小时。
TCHAR szModName[MAX_PATH];
if (!GetModuleFileNameEx(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) // Get the full path to the module's file
continue;
wstring modName = szModName; <-------- this is what im having problem with
根据是否已定义UNICODE
宏,将TCHAR
定义为char
或wchar_t
。 在您的情况下,它似乎未定义,因此您尝试从char[]
数组构造std::wstring
。
我建议您在Windows上始终使用广泛的API,因为那是在此处获得适当Unicode支持的唯一方法:
wchar_t szModName[MAX_PATH];
if (!GetModuleFileNameExW(hProcess, hMods[i], szModName, MAX_PATH))
continue;
wstring modName = szModName;
GetModuleFileNameEx
不是函数,而是一个根据UNICODE
宏扩展为GetModuleFileNameExA
或GetModuleFileNameExW
的宏。
最初,提供了TCHAR
变体,以简化从不支持Unicode的旧Windows版本到具有Unicode支持的基于NT的新Windows版本的过渡。 如今,根本没有理由使用ANSI变体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.