当这部分代码运行时,我收到运行时错误:
void PrintChar(int x, int y, char ch, Colors color) { COORD c = {y,x}; FillConsoleOutputCharacterW(GameData::handle, ch, 1, c, NULL); FillConsoleOutputAttribute(GameData::handle, color, 1, c, NULL); }错误是它覆盖了位置0x00000000
有人可以解释为什么这个错误会出现一个例子吗?
GameData类 -
class GameData { public: static CRITICAL_SECTION cs; static void * handle; static GameStates gameState; static int speed; static int level; static int points; static int nextShape; static int nextTurnShape; };颜色 -
enum Colors { Black, DarkBlue, Green, Blue, Red, Purple, Yellow, While, Gray, LightDarkBlue, LightGreen, LightBlue, LightRed, LightPurple, LightYellow, BrightWhile };PS
如果我需要提供更多代码来澄清正在发生的事情,请告诉我。
I'm getting a runtime error whenver this section of code runs:
void PrintChar(int x, int y, char ch, Colors color) { COORD c = {y,x}; FillConsoleOutputCharacterW(GameData::handle, ch, 1, c, NULL); FillConsoleOutputAttribute(GameData::handle, color, 1, c, NULL); }The error is that it is overwriting location 0x00000000
Could someone explain why this error comes up with an example please?
GameData class -
class GameData { public: static CRITICAL_SECTION cs; static void * handle; static GameStates gameState; static int speed; static int level; static int points; static int nextShape; static int nextTurnShape; };Colours -
enum Colors { Black, DarkBlue, Green, Blue, Red, Purple, Yellow, While, Gray, LightDarkBlue, LightGreen, LightBlue, LightRed, LightPurple, LightYellow, BrightWhile };P.S.
Please let me know if I need to provide any more code to clarify what is going on.
最满意答案
如果您查看该API的文档: https : //msdn.microsoft.com/en-us/library/windows/desktop/ms682663(v = vs。85).aspx
您应该传递一个DWORD变量的地址,该变量接收实际写入控制台屏幕缓冲区的字符数
void PrintChar(int x, int y, char ch, Colors color) { DWORD dwNumOfCharsWritten; COORD c = {static_cast<short>(y), static_cast<short>(x)}; FillConsoleOutputCharacterW(GameData::handle, ch, 1, c, &dwNumOfCharsWritten); FillConsoleOutputAttribute(GameData::handle, color, 1, c, &dwNumOfCharsWritten); }If you look at the documentation of that API: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682663(v=vs.85).aspx
You should pass the address of a DWORD variable that receives the number of characters actually written to the console screen buffer
void PrintChar(int x, int y, char ch, Colors color) { DWORD dwNumOfCharsWritten; COORD c = {static_cast<short>(y), static_cast<short>(x)}; FillConsoleOutputCharacterW(GameData::handle, ch, 1, c, &dwNumOfCharsWritten); FillConsoleOutputAttribute(GameData::handle, color, 1, c, &dwNumOfCharsWritten); }更多推荐
发布评论