Как преобразовать тип WORD в строку в Visual C++
Кто-нибудь может объяснить, как преобразовать слово в строку в C++, пожалуйста?
typedef struct _appversion
{
WORD wVersion;
CHAR szDescription[DESCRIPTION_LEN+1];
} APPVERSION;
// Some code
APPVERSION AppVersions;
// At this point AppVersions structure is initialized properly.
string wVersion;
wVersion = AppVersions.wVersion; // Error
// Error 1 error C2668: 'std::to_string' : ambiguous call to overloaded function
wVersion = std::to_string((unsigned short)AppVersions.wVersion);
1 ответ:
A
WORDв контексте Visual C++ является определением типа дляunsigned short.Таким образом, вы можете использовать
std::to_stringдля этой задачи:wVersion = std::to_string(AppVersions.wVersion);Править: по-видимому, Visual Studio 2010 не поддерживает функции C++11 полностью, вместо этого используйте
std::stringstream:std::stringstream stream; stream <<AppVersions.wVersion; wVersion = stream.str();Обязательно включите
<sstream>
Comments