C++ Vector iterate over abstract class
c++, iterator, sfml, vector
Solution
The problem is that the polimorphism doesn't work at the level of the vectors, but at the levevel of the pointers they contain. So `std::vector<Derived*>` can never be passed as an `std::vector<Base*>`, since they are completely different types. However, you can fill an `std::vector<Base*>` with pointers to `Derived*`.
So, you should declare your bullet vector as
std::vector<Drawable*> bullets;
and fill it with `BulletView*`, for example:
bullets.push_back( new BulletView( some args) );
drawVector(bullets);
Note: In the code above I am sidestepping the issue of dynamically allocated object ownership. Obviously some mechanism must be in place for deleting the `BulletViews` at the right moment.
Problem
Background info: Me and a couple of friends are building this platform game in C++ with the help och sfml and box2d for a school assignment. One of the requirements is that we follow the "MVC pattern". We have created classes like Bullet and Character for the model. And BulletView and CharacterView (both inherits sf::Drawable which is an abstract class) for the view. Instead of duplicating code for drawing and have two methods drawBullets and drawCharacter like this ``` void WorldView::drawBullets() { std::vector<BulletView*>::iterator it; for ( it = bullets.begin() ; it < bullets.end(); it++ ) window->draw(**it); } void WorldView::drawCharacters() { std::vector<CharacterView*>::iterator it; for ( it = characters.begin() ; it < characters.end(); it++ ) window->draw(*it); } ``` I would like a more generic method using polymorhism which would look something like this: ``` void WorldView::drawVector(const std::vector<sf::Drawable*>& vector) { std::vector<sf::Drawable*>::iterator it; for ( it = vector.begin() ; it < vector.end(); it++ ) window->draw(**it); } ``` The bulletView vector is declared like this: ``` std::vector<BulletView*> bullets; ``` Can't get this to work though. And I'm kind of new to C++ so please have mercy! I've tried searching but have not found very specific answers. Errors I get while compiling. Error 8 error C2679: binary '=' : no operator found which takes a right-hand >operand of type 'std::_Vector_const_iterator<_Myvec>' (or there is no acceptable >conversion) c:\users\niklas\multiplaya\sfml test\sfml test\view\worldview.cpp 409 >1 SFML_Test Error 7 error C2664: 'mp::WorldView::drawVector' : cannot convert parameter 1 from >'std::vector<_Ty>' to 'const std::vector<_Ty> &' c:\users\niklas\multiplaya\sfml >test\sfml test\view\worldview.cpp 402 1 SFML_Test