Jump to content
  • Advertisement
Sign in to follow this  
Domarius

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

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

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
Advertisement
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
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!