SOLVED, C++, how do I access the '[ ]' thingy if the vector was passed as a wotsit?
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]
Solution:
But I'm not going to touch on the good and the bad with a long pole. Greetz,
Illco
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
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]
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]
If you want to simply pass the vector data without copying it, then do as ZQF said and pass by reference.
And then to call it:
Create::File(Maze &maze, vector<Character*> &characters, vector<BITMAP*> &tiles);
And then to call it:
create->File(m_Maze, m_Characters, m_Tiles);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement