c++ - C++ 对象数组堆栈溢出
问题描述
当我尝试创建一个'gameObject'数组时-si得到一个堆栈溢出异常,知道可能是什么原因吗?编辑:对于 1 的数组,它不会抛出异常,我错了(只创建一个 'gameObject' 变量就可以了)
我知道我的代码很乱,而且很糟糕,但是我对 c++ 还很陌生,所以请原谅我的代码:(
这是我的 Main.cpp:
int main()
{
using namespace std::literals::chrono_literals;
HWND myconsole = GetConsoleWindow();
HDC mydc = GetDC(myconsole);
bool loop;
loop = false;
std::chrono::steady_clock::time_point start;
std::chrono::steady_clock::time_point end;
std::chrono::duration<float> duration;
gameObject test(mydc, "test.dat");
gameObject objList[100];
test.posX = 200;
test.posY = 10;
std::cout << getCurrentId();
while (true)
{
start = std::chrono::high_resolution_clock::now();
if (GetKeyState(VK_DOWN) & 0x8000)
{
test.move(0, -3);
}
if (GetKeyState(VK_UP) & 0x8000)
{
test.move(0, 3);
}
if (GetKeyState(VK_RIGHT) & 0x8000)
{
test.move(6, 0);
}
if (GetKeyState(VK_SPACE) & 0x8000)
{
gameObject shell(mydc, "shell.dat");
shell.type = 1;
shell.posX = test.posX + test.l;
shell.posY = test.posY + test.h;
objList[getCurrentId()] = shell;
}
if (loop == false)
{
for (int i = 0; i < 100; i++)
{
if (objList[i].type == 1)
{
objList[i].move(1, 0);
}
}
}
if (loop == false)
{
loop = true;
}
else
{
loop = false;
}
end = std::chrono::high_resolution_clock::now();
duration = end - start;
if (duration < 0.0333s)
{
std::this_thread::sleep_for(0.0333s - duration);
}
}
}
这是'gameObject'类:
class gameObject
{
public:
gameObject(HDC currentDc, std::string dataFile);
gameObject();
~gameObject();
void clear();
void draw();
void move(int x, int y);
void loadSprite(std::string spriteName);
bool collide(gameObject);
unsigned short h = 1;
unsigned short l = 1;
int posX;
int posY;
unsigned short type;
COLORREF spriteData[256][256];
unsigned short id;
HDC dc;
};
解决方案
您正在堆栈上创建所有对象gameObject objList[100];
,并且每个对象中都有一个大数组COLORREF spriteData[256][256];
。那是你的堆栈溢出。
使用 astd::vector
来存储您的对象。
推荐阅读
- python - 将带有空格分隔的列表压缩成元组
- shell - Erlang shell输出信息中的$是什么意思
- html - 在我的网络应用程序中,MacOS 搞乱了我的格式,而 Windows 处理得很好
- c# - 从 Windows 应用程序中的表中提取数据
- python - python re正则表达式冻结对随机文本的处理
- visual-studio-code - 如何设置 CodeLLDB 以调试从 Java 进程调用的 Rust 可执行文件?
- excel - 下载保存在烧瓶服务器上的 excel 文件
- tsql - T-SQL 使用 Lead () 函数,但不允许在 GROUP BY 或 UPDATE 中使用
- html - 为什么我的 CSS 动画在除 Safari 之外的所有浏览器中都有效?
- javascript - 比较两个对象数组并在 Javascript 中值为 true 时返回一个新数组