c++ - 如何在Visual C++中将WORD类型转换为字符串

标签 c++ visual-c++

谁能解释一下如何在 C++ 中将 WORD 转换为字符串?

  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); 

最佳答案

一个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>

关于c++ - 如何在Visual C++中将WORD类型转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33016170/

相关文章:

c++ - 无法从头文件访问函数

c++ - 如何用一行代码初始化字符串类型的数组?

c++ - 我如何在 VC++ 中使用 _W64 和 __w64?

c++ - 指针如何引用硬盘中的一个段

c++ - 谁能告诉我为什么 float 不能容纳 3153600000?

c++ - 名称查找和声明点概念

c++ - 尝试传递一个 constexpr lambda 并使用它来显式指定返回类型

c++ - 释放在不同同步上下文中使用的类成员

c++ - Windows API 中的逻辑坐标和设备坐标混淆

c++ - 在 C++ 中读取磁盘中文件的最快方法是什么?