我正在创建一个名为“地图”类型的“ID1000”的对象。该对象包含一个成员“Platforms [] []”。 Platforms [] []是一个二维数组。
class Maps{
int Platforms[][4]
} ID1000;
ID1000.Platforms[0][0] = 100;
在这里,我将Platforms [0] [0]设置为100。 但是,当我执行这行代码时......
al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "TargetY: %f", 800 - TargetY);
object_name [0] [0]突然从100更改为1065353216.此行只是将文本绘制到窗口上的2d图形库Allegro的功能。我不能为我的生活弄清楚为什么它与我的会员的价值有关。
如果您有任何想法或建议,我将不胜感激。
这是完整的程序......
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <math.h>
#include <iostream>
const int WIDTH = 1000;
const int HEIGHT = 800;
class Maps {
public:
int NumPlat;
int MapWidth;
int Platforms[][4];
};
int main(int argc, char **argv)
{
Maps ID1000;
ID1000.Platforms[0][0] = 100;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font18;
float TargetY = 0;
if(!al_init())
return -1;
display = al_create_display(WIDTH, HEIGHT);
if(!display)
return -1;
al_install_keyboard();
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
font18 = al_load_font("BALTH___.ttf", 18, 0);
std::cout << ID1000.Platforms[0][0];
al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "TargetY: %f", 800 - TargetY); //display horizontal position on screen
std::cout << ID1000.Platforms[0][0];
al_destroy_font(font18);
return 0;
}
答案 0 :(得分:2)
数组int Platforms[][4]
为空(它是一个零行数组,每行4个整数)。它在C ++程序中也是无效的。 (这是有效的C,但它被称为“灵活的数组元素”。预期的用途是你使用数组访问语法过度分配对象并在对象之后寻址额外的内存)
访问ID1000.Platforms[0][0]
(第一行中的第一个int)时访问的内存位置在数组之外,实际上在ID1000
对象之外。
此时你处于未定义的行为,任何事情都可能发生。我猜你正在访问的内存位置是指针display
使用的位置,你在其中写下al_create_display()
的结果。