Jump to content
  • Advertisement
Sign in to follow this  

vector pointer nessiary here

This topic is 5039 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 im getting this error: 29 C:\Dev-Cpp\projects\Homeland\Map.cpp cannot convert `std::vector<Tile*, std::allocator<Tile*> >' to `Tile*' in assignment in this function int Map::Set_Equal(const char* SZ_FILENAME,vector<Tile*> *N_TList, vector<Event*> *N_EList) on this line Map_List[0] = N_TList[0]; im pretty sure its because one is a vector pointer and the other one is a vector, my question is how do i get around this or do i need the vector to be a pointer at all in this case? thanks again [smile]

Share this post

Link to post
Share on other sites
Let me try to explain:

Your parameter N_TList is of type vector<Tile*>* - notice the final * indicating that it is a pointer to a vector, rather than an actual vector.

Now by N_TList[0] you probably meant the first element of the vector. You would have gotten it - if N_TList was indeed a vector. It is, however, a pointer, and applying the [] operator to a pointer means to treat it as an array.

So basically, you've retrieved the first element of the array pointed to by N_TList. Since N_TList points to your vector, the return type of this expression is vector<Tile*> (or rather vector<Tile*>& , but don't let this confuse you right now).

Map_List is probably also of type vector<Tile*>. So Map_List[0] is of type Tile*.

See the problem? N_TList[0] is vector<Tile*>, while Map_List[0] is Tile*. This is what the compiler is complaining about.

How to fix this? We need to get the first element of the vector pointed to by N_TList, so we should first dereference N_TList and then apply the [] operator.

Map_List[0] = (*N_TList)[0];

This should work. It tells the compiler to take the first element of the vector, rather than the pointer.

To avoid such problems in the future, you should consider using references instead of pointers when passing vectors as function arguments. If you don't know much about references, you should read up about it somewhere.

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!