Confused about SFML sf::Drawable

I have a function in my main class in my Snake Game SnakeGame::getDrawables which returns a std::vector<sf::Drawable*> which contains all of my drawable objects. However, the function body for this function is not working for me, which is as follows:

1
2
3
4
5
6
7
8
std::vector<sf::Drawable*> SnakeGame::getDrawables() const
{
	std::vector<sf::Drawable*> result;
	result.push_back(&gameMap_);
	result.push_back(&snakeFood_);
	result.push_back(&snake_);
	return result;
}


(All of these objects are derived from the base class sf::Drawable.) I feel like I am missing a basic concept, but do not know what it is:

Here is the error message I am getting, on Visual Studio Community 2017 edition:


Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	E0304	no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=sf::Drawable *, _Alloc=std::allocator<sf::Drawable *>]" matches the argument list	SnakeGame	c:\Users\arnav_1n7er7u\Documents\Visual Studio 2017\Projects\SnakeGame\SnakeGame\SnakeGame.cpp	84	

for each of the three calls.
Since the function getDrawables() is const the members gameMap_ etc. are const as well. So either you remove the const from the function or you return const sf::Drawable*. If the latter works it would be preferable due to the const correctness:
1
2
3
4
5
6
7
8
std::vector<const sf::Drawable*> SnakeGame::getDrawables() const
{
	std::vector<const sf::Drawable*> result;
	result.push_back(&gameMap_);
	result.push_back(&snakeFood_);
	result.push_back(&snake_);
	return result;
}
Thanks! This worked!
Topic archived. No new replies allowed.