visual-c++ - 使用CString::Format时如何防止C6284?

标签 visual-c++ compiler-warnings

在MSVC 2008上使用/analyze编译时,以下代码生成警告C6284:当在调用函数时需要字符串时,对象作为参数'%s'传递。

 CString strTmp, str;
 str = L"aaa.txt"
 strTmp.Format (L"File: %s", str);

我正在为此寻找一个很好的解决方案,不需要static_cast

最佳答案

Microsoft用可变参数函数here描述了CString的用法:

CString  kindOfFruit = "bananas";
int      howmany = 25;
printf_s( "You have %d %s\n", howmany, (LPCTSTR)kindOfFruit ); 

另外,您也可以使用PCXSTR CString::GetString() const;方法尝试修复警告:
CString strTmp, str;
str = L"aaa.txt"
strTmp.Format (L"File: %s", str.GetString());

关于visual-c++ - 使用CString::Format时如何防止C6284?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2598111/

相关文章:

c++ - 为什么对对象生成器调用的右值引用需要复制构造函数?

winapi - CCombobox : how to set text color of Editable area text when combobox style is dropdown in win32

c++ - 错误 C2220 : warning treated as error - no 'object' file generated

c++ - -Wstrict-函数定义中的溢出警告

qt - 如何为 Windows 安装开源 Qt 库 5 二进制版本

windows - 如何在 Windows 中获取或更改已断开连接的 NIC 的 IP 地址?

c++ - RVO 是否适用于对象成员?

c - 警告 : control reaches end of non-void function In User-Defined Function

c++ - 为什么 GCC 中的 -Wunused-variable 即使在静态常量上也会产生错误?

c - gcc:如何避免对程序集中定义的函数发出 "used but never defined"警告