C++ 错误:由于 swprintf 而从 'int' 到 'const wchar_t* 的转换无效

C++ error:invalid conversion from 'int' to 'const wchar_t* because of swprintf

本文关键字:wchar 无效 转换 const int 错误 由于 swprintf C++ 而从      更新时间:2023-10-16

我要编译的文件

第一个错误:

无法将"wchar_t*"转换为"LPCSTR" {又名"const char*"}|

我通过将WriteConsoleOutputCharacter更改为WriteConsoleOutputCharacterW来修复它。

但是,我在此行上收到另一个错误:

swprintf(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);

从"int"到"const wchar_t*"的转换无效 [-允许]| 初始化 'int swprintf(wchar_t*, const wchar_t*, ...(' 的参数 2

|

如何编译此代码?

您使用的swprintf函数似乎没有缓冲区大小。您可能需要考虑改用_snwprintf

_snwprintf(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);

您可能会收到此函数不安全的警告 - 建议的替代方法是_snwprintf_s,您必须指定缓冲区的大小要写入的最大字符数。在实践中,这些通常是相同的,因此大多数时候,对此函数的调用如下所示:

_snwprintf_s(screen, 40, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);