Sign in to follow this  
raptorstrike

vector pointer nessiary here

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

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