I have this, in my header:
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;
I then initialize the worldSurface in the .cpp:
WorldSurface *worldSurface = new WorldSurface[MAX_LEVELS];
And this function, I can’t get to work no matter what, have tried messing around with = NULL, pointers, the -> instead of .’s… (do have in mind I’m not very savvy of pointer subjects)
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++;
}
It’s 10 worldSurfaces, each containing 50 surface structs, and I want to acess the struct, which is inside the worldSurface[drawLevel], and the surface struct I want to access is known in the worldSurface, in the .counter variable. But all of the acesses to the underlying surface struct fail with segmentation fault, and I have no clue why…
Thanks for the help!
Your
WorldSurfaceisn’t defined nor initialized properly:You can’t initialize your data in place along with the declaration of the class. You need to do this in a constructor, e.g.: