Jump to content
  • Advertisement
Sign in to follow this  
darenking

SOLVED! Allegro, how can I pass a pointer or whatever to a vector of BITMAPs?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again all. I have a vector of BITMAPs, a member of my World class, called: std::vector<BITMAP*> m_Sprites; I have a function that loads in a .bmp file and chops it up, storing the images as BITMAPs in the vector. It all works perfectly well. But I've realised that I need the function to be more flexible, I need it to be able to store BITMAPs in various different vectors. The World object should send it a pointer or whatever to the vector that it needs the BITMAPs to be stored in. I've tried sending it the vector like this:
CreateSpritesLoad(file, m_Sprites)

...

bool World::CreateSpritesLoad(std::vector<BITMAP*> passedVector)
{
//load in the .bmp file etc here
	passedVector.push_back( create_bitmap(width, height) );
	blit( fromBitmap, passedVector.back(), x, y, 0, 0, width, height );
//etc
}


...but the program crashes horribly. It compiles fine. What am I doing wrong? Apart from getting out of bed in the morning :{ [Edited by - darenking on July 28, 2005 6:30:13 AM]

Share this post


Link to post
Share on other sites
Advertisement
Well, the parameter is just a local variable destroyed when the function returns. You either need to make it a pointer or (preferably) a reference.

Share this post


Link to post
Share on other sites
So I just send the address of it, like this?

CreateSpritesLoad(file, &m_Sprites)

As usual with these things, I get the concept (more or less) but can't do the syntax.

Share this post


Link to post
Share on other sites
Close, but not quite. For passing by reference, the only thing you need to modify is the function declaration. In this case, it would be:

bool World::CreateSpritesLoad(std::vector<BITMAP*>& passedVector)

Note the '&' right after the type -- this indicates a pass by reference. You don't need to change how you actually call the function, which is the nice thing -- you can just do "CreateSpritesLoad(file, m_Sprites)" as before.

That is how you pass by reference in C++. I'm not entirely certain this would fix what's causing your program to crash, however...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!