我有一个程序需要使用 Format();将字符串文字和 int 组合成 CString 变量的函数。我尝试了几种不同的方法,它们的代码在这里:
// declare variables used
CString _CString;
int _int;
// try to use format function with string literal
_CString.Format("text",_int);
// try to use format function with C-Style cast
_CString.Format((wchar_t)"text",_int);
// try to use format function with static_cast
_CString.Format(static_cast<wchar_t>("text"),_int);
第一个返回错误 C2664:“void ATL::CStringT::Format(const wchar_t *,...)”:无法将参数 1 从“const char [33]”转换为“const wchar_t *”
对于第二个,没有错误,但文本显示为汉字。
第三个返回错误 C2440:“static_cast”:无法从“const char [33]”转换为“wchar_t”
将 CString 转换为 wchar_t *s 有什么想法吗?
谢谢
最佳答案
好吧,目前还不完全清楚您想定位哪种字符串类型,但这是我要做的:
_CString.Format(_T("%d"), _int);
尝试将多字节字符串类型转换为 Unicode 字符串可能会编译,但这是自找麻烦,因为它仍然是多字节字符串。如果需要,您需要转换整个字符串,而不仅仅是类型转换。
关于c++ - 在 C++ 中转换类型时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4611514/