It compiles, but errors out |
I'm sorry, I'm a little confused. Does it compile? Or do you get an error?
By error do you mean a runtime bug or a crash? I take "error" to mean a compiler error ... which would mean it isn't compiling. So I'm not quite sure what's really going on.
I guess the compiler might be warning you about this:
1 2 3 4
|
for(
row ; // <- this does nothing. It is pointless to say "row" here.
row < 2;
row += 1 )
|
Solutions for this would be to initialize the row variable in the loop:
for( row = 0; row < 2; row += 1)
Or better yet...
declare and initialize the row variable in the loop:
for( int row = 0; row < 2; row += 1)
(then get rid of the row declaration a few lines up)
That'll solve the warning, anyway. But I don't think the warning is related to your problems.
You still have the "overwrite" problem. And you are creating 6 enemies, not 5:
|
for(int unsigned i = 0 ;i <= 5; i++ )
|
This loop iterates
six times. Once for each of these values: 0, 1, 2, 3, 4, 5
Again, you do not want <=, here. You want <.
You also are still overwriting the first row opponents.
opponent[i] = new Enemy(enemyX,enemyY);
For the first row... when i==0, you will be assigning opponent[0].
Then for the 2nd row, when i==0 you will be assigning opponent[0] AGAIN. This causes you to
lose the first row's opponent[0], and have it replaced with the 2nd row's.
Your other problem is here:
1 2 3 4
|
for(int unsigned j = 1; j < opponents; j++)
{
SDL_FillRect(screen,&opponent[j]->box,blue);
}
|
You shouldn't be starting j at 1, because arrays start at 0. (this is probably why one of your items was not drawing before -- you were skipping over the first one)
You also are looping 'opponents' (10) times, but you only put 6 opponents in your 'opponent' array.
So opponent[6], opponent[7], opponent[8] and opponent[9] are all bad pointers. And attempting to read from a bad pointer's 'box' member is likely to cause your program to crash.