void spawn_enemies(vector<Enemy>& enemies) {
I had that function, and it worked just fine to pass a vector of Enemy’s as an argument.
However, I know have a vector of <Enemy*>‘s, and it doesn’t work so well, and I also tried to use:
void spawn_enemies(vector<Enemy*>& enemies) {
But it didn’t work either, I get the following error on compilation:
src/Paxlure.cpp:32:28: error: no matching function for call to ‘std::vector<Enemy*>::push_back(Enemy&)’
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:826:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Enemy*, _Alloc = std::allocator<Enemy*>, std::vector<_Tp, _Alloc>::value_type = Enemy*]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:826:7: note: no known conversion for argument 1 from ‘Enemy’ to ‘Enemy* const&’
Thank you
If you have a
vectorof pointers, you need topush_backpointers, not objects.versus
In the latter case, with dynamically allocated memory, you’ll need to free it yourself:
delete pe;. But you have to make sure you correctly manage the memory – don’t delete it before you’re sure thevectorwill no longer try to work with it. Or simply use smart pointers instead of raw ones.