当我编译我的 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/