Sign in to follow this  
Domarius

Don't understand how to get a value from a vector

Recommended Posts

This is my vector;
std::vector<GameDatabaseItem> m_gameDatabase;

This is the function that is used to get a value from it.
const GameDatabaseItem &GameDatabase::GetItem(int index)
{
	return m_gameDatabase[index];
}

This is the variable I want to store the value;
GameDatabaseItem *game;

In a for loop, this is where I try to get that value and put it in that variable
game = gameDatabase->GetItem(i);

And on that line, this is the error I get; error C2440: '=' : cannot convert from 'const GameDatabaseItem' to 'const GameDatabaseItem *' Why doesn't it like it?

Share this post


Link to post
Share on other sites
Because your variable is a pointer to GameDatabaseItem, the vector stores actual GameDatabaseItem objects.

Try this:

game = &gameDatabase->GetItem( i );


Caution: If you add or remove any element from the vector your pointer will most likely be corrupt because the element in the container might have been invalidated.

Share this post


Link to post
Share on other sites
Thankyou so much. I understand it now that you tell me - using & gets the address of the object itself.

I assumed that the vector would return the address anyway. My memory is refreshed - some object type was being passed around by value (and not by address) in a game engine my lecturer developed. But I'm not sure how to do this and not sure why I would need to.

I can count on this forum. I said to myself - I'll post this problem, have a shower, and when I get back, it'll be answered. It was, heheh.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this