我的标题中有这个:
struct Surface {
char *objectName;
int xPos;
int yPos;
SDL_Surface *surface;
};
struct WorldSurface {
Surface *surface = new Surface[MAX_SURFACES];
int counter = 0;
int current = 0;
};
WorldSurface *worldSurface;
然后,我在.cpp:中初始化worldSurface
WorldSurface *worldSurface = new WorldSurface[MAX_LEVELS];
这个函数,我无论如何都无法工作,我试着用=NULL、指针、->而不是s…(记住了,我对指针主题不是很了解)
void drawClass::addSurface(char* objectName, char* surfaceFile, int xPos, int yPos, int drawLevel) {
int cnt = worldSurface[drawLevel].counter;
worldSurface[drawLevel].surface[cnt].objectName = objectName;
worldSurface[drawLevel].surface[cnt].surface = load_image(surfaceFile);
worldSurface[drawLevel].surface[cnt].xPos = xPos;
worldSurface[drawLevel].surface[cnt].yPos = yPos;
worldSurface[drawLevel].counter++;
}
它是10个worldSurfaces,每个包含50个曲面结构,我想访问位于worldSurface[drawLevel]内部的结构,我要访问的曲面结构在worldSurface中的.counter变量中是已知的。但所有对底层表面结构的访问都因分割错误而失败,我不知道为什么。。。
谢谢你的帮助!
您的WorldSurface
未正确定义或初始化:
struct WorldSurface {
Surface *surface = new Surface[MAX_SURFACES];
int counter = 0;
int current = 0;
};
不能将数据与类的声明一起初始化到位。您需要在构造函数中执行此操作,例如:
struct WorldSurface {
Surface *surface;
int counter;
int current;
};
...
WorldSurface::WorldSurface():
counter(0),current(0),surface(new Surface[MAX_SURFACES]){}