Jump to content
  • Advertisement
Sign in to follow this  
darenking

SOLVED, C++, how do I access the '[ ]' thingy if the vector was passed as a wotsit?

This topic is 4838 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

OK, so I'm not very good at terminology, but I know what I mean. I have an object, World, that has vectors of various different things, and I'm creating a new object called Create that loads data from a file and uses the data to create objects in the World object. Anyway, the World object passes the addresses of some of its objects to Create like this: Create* create = new Create(); // I assume this is the best way to create the object? create->File(&m_Maze, &m_Characters, &m_Tiles); These objects are members of World, so in world.h we have: Maze m_Maze; vector<Character*> m_Characters; vector<BITMAP*> m_Tiles; //Allegro bitmaps They are received by Create like this: Create::File(Maze* const maze, vector<Character*>* const characters, vector<BITMAP*>* const tiles); (If anyone thinks I've done this wrong let me know but it seems to work...) And stored in Create like this: m_pMaze = maze; m_pCharacters = characters; m_pTiles = tiles; This all works fine, and I can add tiles to the m_pTiles vector like this: m_pTiles->push_back( create_bitmap( TILE_WIDTH, TILE_HEIGHT ) ); But I get an error about 'cannot convert' something or other when I try access elements in the vectors as though they were arrays: m_pTiles[tile] eg using Allegro drawing commands: putpixel( m_pTiles[tile], x, y, colour ); I'm sure there must be a simple explanation! [Edited by - darenking on August 20, 2005 1:45:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
Solution:

putpixel( m_pTiles->at( tile ), x, y, colour );


But I'm not going to touch on the good and the bad with a long pole. Greetz,

Illco

Share this post


Link to post
Share on other sites
you can't use the bracket operator on a pointer (well... yeah but it is not what you want).

What you need to do is dereference the pointer, then you can use its member functions, and bracket is just a function like any other.

(*m_pTiles)[tile]

Share this post


Link to post
Share on other sites
You could always pass by reference unless there's a good reason not to.

Share this post


Link to post
Share on other sites
If you want to simply pass the vector data without copying it, then do as ZQF said and pass by reference.
Create::File(Maze &maze, vector<Character*> &characters, vector<BITMAP*> &tiles);

And then to call it:
create->File(m_Maze, m_Characters, m_Tiles);

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!