Public Group

# vector pointer nessiary here

This topic is 4951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Can you show what map_list is.

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

Jim.

##### 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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631442
• Total Posts
3000089
×