I am making a game using C++ with SFML lib and in my main I created couple functions. I know it is a bad manner to do that in main but yeah. when I try to compile the code I am getting error:
src/main.cpp: In function 'void create_platforms(std::vector<std::unique_ptr<sf::Sprite> >&)':
src/main.cpp:12:6: error: no previous declaration for 'void create_platforms(std::vector<std::unique_ptr<sf::Sprite> >&)' [-Werror=missing-declarations]
void create_platforms(std::vector<std::unique_ptr<sf::Sprite>>& platforms)
^~~~~~~~~~~~~~~~src/Player.cpp:
In member function 'void Player::gravity(sf::Time&, sf::FloatRect&)':
same goes to create_items and create_interface. I am not using any header file for main.cpp, can I just declare those in main.cpp? If so I would be glad if someone would help me how to do it.
void create_platforms(std::vector<std::unique_ptr<sf::Sprite>>& platforms)
{
[...]
}
void create_items(std::vector<std::unique_ptr<sf::Sprite>>& healing_items)
{
auto small_heart = std::make_unique<sf::Sprite>();
small_heart->setTexture(*TextureManager::getTexture("heart"));
small_heart->setPosition(320, 180);
small_heart->setScale(0.4, 0.4);
healing_items.emplace_back(std::move(small_heart));
}
void create_interface(std::vector<std::unique_ptr<sf::Sprite>>& interface)
{
auto life = std::make_unique<sf::Sprite>();`enter code here`
life->setTexture(*TextureManager::getTexture("life"));
life->setPosition(10, 530);
interface.emplace_back(std::move(life));
}
int main()
{
[...]
}
can I just declare those in main.cpp?
Technically yes. What matters for the -Wmissing-declarations option is that a declaration is before definition. The file in which you put the declaration is irrelevant.
Note however that this may be against the spirit of the warning. It is unclear why you want to get warnings about missing declarations when you simultaneously want to not use a header.
If the function is intended to be used only within that translation unit, then you should declare it within an anonymous namespace. -Wmissing-declarations will not warn about functions in anonymous namespace.