在MessageBox中显示一个int变量

use*_*025 3 c++ winapi

我正在研究用Visual C++ 6.0编写的旧应用程序.我试图intMessageBox调试中显示一个变量.这是我的代码,我认为这将是一个简单的过程,但我只是在学习C++.评论的两行我也尝试过类似的错误.以下是我得到的错误.

int index1 = 1;
char test1 = index1;
// char var1[] = index1;
// char *varGo1 = index1;
MessageBox(NULL, test1, "testx", MB_OK);
Run Code Online (Sandbox Code Playgroud)

错误C2664:'MessageBoxA':无法将参数2从'char'转换为'const char*'

Iva*_*rop 8

如果你标记C++,为什么要烦扰C风格的字符串呢?

尽管Mark Ransom提供了MFC解决方案(这是完全有效的),但这里有一个标准的C++ 解决方案:

int index1 = 1;
std::string test1 = std::to_string(index1);
MessageBoxA(NULL, test1.c_str(), "testx", MB_OK);
Run Code Online (Sandbox Code Playgroud)

参考文献:

使用boost::format更复杂的格式.


Mar*_*som 4

CString str1;
str1.Format(_T("%d"), index1);
MessageBox(NULL, str1, "testx", MB_OK);
Run Code Online (Sandbox Code Playgroud)

CString 的Format工作原理就像printf用参数列表填充字符串一样。