vector pointer nessiary here

Started by
1 comment, last by randomZ 19 years, 4 months ago
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]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
Can you show what map_list is.

Is it std::vector<Tile*>, std::Vector<Tile>, etc.

Jim.
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.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/

This topic is closed to new replies.

Advertisement