[英]_CrtisValidHeapPointer(block) error using SetConsoleCursorPosition function
Develop console Snake.开发控制台 Snake。 At the end of the program gives an error _CrtisValidHeapPointer(block).在程序结束时给出错误 _CrtisValidHeapPointer(block)。 Found out by experience, that the problem is in SetConsoleCursorPosition function in line 32根据经验发现,问题出在第 32 行的 SetConsoleCursorPosition function
#include <iostream>
#include <Windows.h>
#include "Drawer.h"
#include "Snake.h"
using namespace std;
int main()
{
Drawer BasicImage;// создание объекта класса основной картинки игры
Snake Player;
short pre_x_pos,
pre_y_pos;
bool game_over = false;
HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
BasicImage.ShowConsoleCursor(false);
BasicImage.FormField();
SetConsoleCursorPosition(console_handle, { 0, 0 });
BasicImage.FormWalls();
while (!game_over)
{
Player.MoveSet();
pre_x_pos = Player.x_pos;
pre_y_pos = Player.y_pos;
SetConsoleCursorPosition(console_handle, { pre_x_pos, pre_y_pos });
cout << BasicImage.floor_skin;
SetConsoleCursorPosition(console_handle, { Player.x_pos, Player.y_pos });
cout << Player.snake_skin;
if (BasicImage.field[Player.x_pos][Player.y_pos] == BasicImage.wall_skin)
{
game_over = true;
}
Sleep(500);
}
SetConsoleCursorPosition(console_handle, { 0, BasicImage.height });
cout << "Game Over";
return 0;
}
The compiler doesn't make any error message,I can easily launch the app, but when I close the main window,_CrtIsValidHeapPointer(block) error was raised编译器没有发出任何错误消息,我可以轻松启动应用程序,但是当我关闭主 window 时,引发了 _CrtIsValidHeapPointer(block) 错误
Previously, 'pre_x_pos' and 'pre_y_pos' was variables belonging to the 'Snake' class. I tried to move it to main file, but these solution didn't help.以前,'pre_x_pos' 和 'pre_y_pos' 是属于 'Snake' class 的变量。我试图将它移动到主文件,但这些解决方案没有帮助。
UPD: content of all other files: Drawer.h: UPD:所有其他文件的内容:Drawer.h:
class Drawer
{
public:
short height = 0,
length = 0;
char wall_skin = '#';
char floor_skin = '.';
char** field = 0;
void ShowConsoleCursor(bool);
void FormField();
void FormWalls();
};
Snake.h:蛇.h:
class Snake
{
public:
short x_pos = 1,
y_pos = 4,
pre_x_pos = 0,
pre_y_pos = 0,
x_scale = 0,
y_scale = 0,
last_dir = 0;
char snake_skin = '@';
void MoveSet();
void MoveLeft();
void MoveRight();
void MoveUP();
void MoveDown();
};
Drawer.cpp:抽屉.cpp:
#include <iostream>
#include <Windows.h>
#include "Drawer.h"
using namespace std;
void Drawer::ShowConsoleCursor(bool show_flag)
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = show_flag; // set the cursor visibility
SetConsoleCursorInfo(out, &cursorInfo);
}
void Drawer::FormField()
{
cin >> height;
cin >> length;
field = new char*[height];
for (int i = 0; i < height; i++)
{
field[i] = new char;
}
}
void Drawer::FormWalls()
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < length; j++)
{
if (i == 0 || i == height - 1 || j == 0 || j == length - 1)
{
field[i][j] = wall_skin;
cout << field[i][j];
}
else
{
field[i][j] = floor_skin;
cout << field[i][j];
}
}
cout << endl;
}
}
Snake.cpp:蛇.cpp:
#include <Windows.h>
#include "Snake.h"
void Snake::MoveSet()
{
if (GetAsyncKeyState(0x57) && last_dir != 3)
{
Snake::MoveUP();
last_dir = 1;
}
else if (GetAsyncKeyState(0x44) && last_dir != 4)
{
Snake::MoveRight();
last_dir = 2;
}
else if (GetAsyncKeyState(0x53) && last_dir != 1)
{
Snake::MoveDown();
last_dir = 3;
}
else if (GetAsyncKeyState(0x41) && last_dir != 2)
{
Snake::MoveLeft();
last_dir = 4;
}
x_pos += x_scale;
y_pos += y_scale;
}
void Snake::MoveDown()
{
x_scale = 0;
y_scale = 1;
}
void Snake::MoveRight()
{
x_scale = 1;
y_scale = 0;
}
void Snake::MoveLeft()
{
x_scale = -1;
y_scale = 0;
}
void Snake::MoveUP()
{
x_scale = 0;
y_scale = -1;
}
I made a mistake when allocating memory:我分配memory的时候出错了:
void Drawer::FormField()
{
cin >> height;
cin >> length;
field = new char*[height];
for (int i = 0; i < height; i++)
{
field[i] = new char[];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.