I have a class Monster for NPCs and so on in the game, and I want to create a vector containing all the currently active monsters. As they die or get spawned I figure I'll pushback or popback them in or out of the vector.

Anyway, to test that it works I set up the following:


Code:
#include <iostream>
#include <vector>

void drawimage(SDL_Surface *srcimg, int sx, int sy, int sw, int sh, SDL_Surface *dstimg, int dx, int dy, int alpha);
void render();
Monster Monster1;
Monster Monster2;
vector<Monster> MonsterList (30);

...


int main(int argc, char *argv[])
{

    MonsterList[0]=Monster1;
    MonsterList[1]=Monster2;

...

render()

}

void render()
{
...

    drawimage(image, 0, 0, MonsterList.at(0).spritew, MonsterList.at(0).spriteh, screen, MonsterList.at(0).xpos-screenx, MonsterList.at(0).ypos-screeny, 255);
    drawimage(image, 0, 0, MonsterList.at(1).spritew, MonsterList.at(1).spriteh, screen, MonsterList.at(1).xpos-screenx, MonsterList.at(1).ypos-screeny, 255);
}
Drawimage is just a function that uses SDL to blit the monster graphic to the screen. I'm passing it the x,y coordinates and height/width of each Monster.

Basically when I referenced Monster1 and Monster2 directly everything was fine, but when I put the vector in and referenced them from the vector I don't get any error but the monsters don't display on screen.

I tried a few different combinations, I tried using pushback originally instead of an array reference but that didn't work either, the interesting thing though is that if I put the two MonsterList[0]=Monster1 lines into the render step it works. Is this some kind of scope issue?

The reason I'm passing the individual components of Monster rather than the whole object is that I also pass things to drawimage() that aren't Monsters or even related classes.