c++ - 警告 C4477 'wprintf_s' : format string '%s' requires an argument of type 'wchar_t *'

标签 c++ visual-studio c++11

当我编译我的 C++ 项目时,我收到以下警告消息:

Warning C4477 'wprintf_s' : format string '%s' requires an argument of type 'wchar_t *', but variadic argument 1 has type std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>

调用时出错,

wstring(featureList)

下面是代码片段:

CString featureList;

MyLog(L"featureList1.Replace(restoken + L\"; \", L\"= 1; \") list : %s",wstring(featureList));

当我如下所示使用 c_str() 进行转换时,警告就解决了。

wstring(featureList).c_str()

我可以继续进行此修复吗?

最佳答案

是的,这是正确的。 wstring 是临时的,但它的生命周期仅在 wsprintf_s 返回后结束。

作为 MSVC++ 扩展,您还可以完全省略 wstring 并按原样传递 featureList。在标准 C++ 中,这是未定义的行为,但 Microsoft 编写了 CString 以便与他们自己的编译器一起工作。

关于c++ - 警告 C4477 'wprintf_s' : format string '%s' requires an argument of type 'wchar_t *' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57975046/

相关文章:

c++ - C++中的基本问题

c++ - 15 位颜色分量,位移位

windows - 将 32 位项目升级到 64 位后启动应用程序失败,错误代码为 0xc000007b

visual-studio - 使用 editoconfig 在 Visual Studio 下强制 const 的大写

c++ - 有没有办法将数据从 vector 移动到动态数组?

C++协程用于使生成器永不停止

c++ - 静态多态性中的歧义第 2 部分

c++ - Visual Studio 2010 和 2012 中 STL 容器字节大小的差异

c++ - Container::value_type 的模板特化

c++ - 为什么 move ctor 比 copy ctor 慢?