# Problem creating levels using inherited classes/polymorphism

I'm trying to write my level classes by having a base class that each level class inherits from...The base class uses pure virtual functions.

My base class is only going to be used as a vector that'll have the inherited level classes pushed onto it...This is what my code looks like at the moment, I've tried various things and get the same result (segmentation fault).

//level.h
class Level
{
protected:
Mix_Music *music;
SDL_Surface *background;
SDL_Surface *background2;

vector<Enemy> enemy;

int time;
public:
Level();
virtual ~Level();

int bgX, bgY;
int bg2X, bg2Y;
int width, height;

virtual void update();
virtual void draw();
};
//level.cpp
Level::Level()
{
bgX = 0;
bgY = 0;
bg2X = 0;
bg2Y = 0;
width = 2048;
height = 480;

time = 0;
}
Level::~Level()
{
}
//virtual functions are empty...


I'm not sure exactly what I'm supposed to include in the inherited class structure, but this is what I have at the moment...

//level1.h
class Level1: public Level
{
public:
Level1();
~Level1();

void update();
void draw();
};
//level1.cpp
Level1::Level1()
{
}
Level1::~Level1()
{
enemy.clear();

Mix_FreeMusic(music);
SDL_FreeSurface(background);
SDL_FreeSurface(background2);

music = NULL;
background = NULL;
background2 = NULL;

Mix_CloseAudio();
}

{

Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 4096);
Mix_PlayMusic(music, -1);
}
{
}
//functions have level-specific code in them...


Right now for testing purposes, I just have the main loop call Level1 level1; and use the functions, but when I run the game I get a segmentation fault. This is the first time I've tried writing inherited classes, so I know I'm doing something wrong, but I can't seem to figure out what exactly.

• Here's a tip about a common C++ rookie mistake you're making. Since you intend to use polymorphism, mark ~Level() as virtual too or you will get into problems when trying to call delete on a Level* pointer. – David Gouveia Jan 15 '12 at 23:48
• Did you mean to NULL out your pointers in Level1::~Level1 before calling their free functions? – M Jellyfish Jan 16 '12 at 0:14
• I didn't mean to...But I already fixed it and I still have the same problem. – Adam Jan 16 '12 at 0:21
• What are the "functions have level-specific code in them..."? if the code is just as it is in the question, you doesnt really need to use polymorphism. – Gustavo Maciel Jan 16 '12 at 0:44
• This seems... really over-designed. Why do you need to have a new class for each level? That will make it really hard and annoying to add more levels in the future. Ideally, you should have a single level class that can handle loading a variety of different levels. – thedaian Jan 16 '12 at 0:47

You're doing it wrong. Making a class for each level? No, levels are data, not code. Make a generic Level class, and then load .level (or whatever) files into it. The files then should just have a list of all the resources that need to be loaded, a list of events/scripts, and so on.
Why? Because then you can make a separate level editor, that creates the .level files for you, and you don't even have to touch your game's code (which generates bugs and so on).