Public Group

# Having some Vector Pointer problems.

This topic is 4059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Alright, I've got a Vector which holds all of the sprites currently existing. The plan is to then, one element at a time, draw each sprite in it's proper location: Vector Initialization
vector<Sprite> sprites;

// Declare each Sprite to be used in the game.
Ball testBall(gameGraphics, "texture/ball.bmp", gameInput, gameLoopTimer, 0.0, 0.0, 0.0, 0.0);
Sprite greyBrick(gameGraphics, "texture/brick.bmp", 100.0, 100.0, 17.0, 11.0);

// Then assign each of the declared Sprites to the sprites vector.
sprites->insert( sprites->end(), testBall );
sprites->insert( sprites->end(), greyBrick );

You can ignore the specifics of Sprite and Ball objects declarations. Those are not causing problems. Ball is simply derived from Sprite. This is what I'm trying to do:
sprites.at(0).Draw();

Draw() is a function of all Sprites. It draws them to the screen. I've tested Draw(), and it's working fine. I can draw a sprite using this funtion for any plain sprite I declare. The problem is happening when I try to draw one of the sprites in my vector... As far as I can tell, this code should work, but when I run it, the program crashes and closes. I thank you all in advance for any help you may provide =) Meow.

##### Share on other sites
Ball is getting sliced, as the vector is designed to hold only Sprite objects, as opposed to pointers to Sprite objects (which would then work properly with inheritance).

What you need to do is:

// Creationtypedef vector<Sprite*> sprites;Ball *testBall = new Ball (gameGraphics, "texture/ball.bmp", gameInput, gameLoopTimer, 0.0, 0.0, 0.0, 0.0);Sprite *greyBrick = = new Sprite (gameGraphics, "texture/brick.bmp", 100.0, 100.0, 17.0, 11.0);sprites.push_back (testBall);sprites.push_back (greyBrick);// Drawing (can also replace with iterators)for (int sprite_index = 0; sprite_index < m_sprites.size (); ++sprite_index)    sprites[sprite_index]->Draw ();// Remove all objectsfor (int sprite_index = 0; sprite_index < m_sprites.size (); ++sprite_index)    delete sprites[sprite_index];sprites.clear ();// To remove a specific objectvoid Remove (int index){    delete sprites[index];    sprites.erase (sprites.begin () + index);}

As you can see, you'll need to manage the deletion of the allocated memory yourself. An alternative is to store a vector of boost::shared_ptr, but that's probably a bit beyond your scope at the moment. Whatever you do, do NOT store a vector of auto_ptrs, as Things Go Wrong.

Edit: an explaination of the above code

What the code above does is store pointers to Sprite objects (which in-turn allows you to store pointers to any object derived from Sprite). You then have to create each object on the heap (i.e. by calling "new Ball" or "new Sprite"), and then add the pointer to the list.

When it comes to deleting the elements in the vector, you must remember to delete the object allocated with "new", hence we call "delete" on the element before erasing it from the vector to ensure that no memory leaks occur.

##### Share on other sites
Quote:
 Original post by MENTALBall is getting sliced, as the vector is designed to hold only Sprite objects, as opposed to pointers to Sprite objects (which would then work properly with inheritance).What you need to do is:*** Source Snippet Removed ***As you can see, you'll need to manage the deletion of the allocated memory yourself. An alternative is to store a vector of boost::shared_ptr, but that's probably a bit beyond your scope at the moment. Whatever you do, do NOT store a vector of auto_ptrs, as Things Go Wrong.Edit: an explaination of the above codeWhat the code above does is store pointers to Sprite objects (which in-turn allows you to store pointers to any object derived from Sprite). You then have to create each object on the heap (i.e. by calling "new Ball" or "new Sprite"), and then add the pointer to the list.When it comes to deleting the elements in the vector, you must remember to delete the object allocated with "new", hence we call "delete" on the element before erasing it from the vector to ensure that no memory leaks occur.

I wont need to delete them until the program exists, becuase it's a vector I use over and over again; however, thank you for putting it in. I'm still getting used to memory and I would have more or less forgotten to clear it all =P

I'm getting an error with push_back, though. It's not liking the fact that I'm passing it pointers... which, is what testBall and greyBrick are now.

(also, a side note, how do you put code in those scrolling boxes? That's very clean looking.)

Edit: Never mind on the push_back issue. I was being stupid, I forgot to change my declaration of sprites =P

Mew.

##### Share on other sites
Quote:
 I wont need to delete them until the program exists, becuase it's a vector I use over and over again;

That's irrelevant; you must get used to delete everything you new,even if you do it in the end of the program,don't realy on the OS to clear the memory for you after the process exits. Or you can use boost's smart pointers that do it for you.

##### Share on other sites
Quote:
Original post by mikeman
Quote:
 I wont need to delete them until the program exists, becuase it's a vector I use over and over again;

That's irrelevant; you must get used to delete everything you new,even if you do it in the end of the program,don't realy on the OS to clear the memory for you after the process exits. Or you can use boost's smart pointers that do it for you.

I know. I mentioned that I was glad he included it, though, because I'd still need to delete them later on.

I have another problem that hopefully someone can help me with:

I'm trying to move all of my sprites at the same time, similar to how MENTAL coded the drawing of each sprite. The only difference is, Sprites and Balls move differently; that is, Sprites don't move at all, and Balls bounce around. I came across virtual functions, and am under the impression that this is the best way to go with it.

For whatever reason though, when I declare them, I keep getting a compile error saying: "In Sprite.h: variable or field Move() declared void". Normally I'd expect this if I was trying to return something, but I'm not... Here's the sections in each class:

In Sprite.h
virtual void Move(vector<Sprite*> *gameSprites);

In Sprite.cpp
virtual void Sprite::Move(vector<Sprite*> *gameSprites)
{
// do nothing, return nothing.
}

In Ball.h
void Move(vector<Sprite*> *gameSprites);

In Ball.cpp
void Ball::Move(vector<Sprite*> *gameSprites)
{
// Do the stuff that moves a ball
// I don't return anything!
}

Everything looks A-ok to me... Any ideas?

Edit: It turns out it was a simple syntax error. I had forgotten to include "using namespace std;" in those two files. After adding the std:: to vector, everything worked fine. The compiler was just horribly confused =P

Meow.

[Edited by - KuroKitten on May 9, 2007 8:27:18 PM]

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631368
• Total Posts
2999596
×