I have a header which defines some functions I need. I include them in two files which are themselves included in main.cpp, but I get the multiple definitions of function error, despite the fact that I put
#ifndef MYFUNCTION_H
#define MYFUNCTION_H
//code
#endif
in my code. So what did I do wrong? Here is my header:
#ifndef EXTRASFMLFUNCTIONS_H
#define EXTRASFMLFUNCTIONS_H
#include <SFML/System/Vector2.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Text.hpp>
inline void setCenterPos(sf::Sprite &entity, const sf::Vector2f& position)
{
entity.setPosition(sf::Vector2f{(position.x - entity.getGlobalBounds().width / 2), position.y - (entity.getGlobalBounds().height / 2)});
}
inline void setCenterPos(sf::Text &entity, const sf::Vector2f& position)
{
entity.setPosition(sf::Vector2f{(position.x - entity.getGlobalBounds().width / 2), position.y - (entity.getGlobalBounds().height / 2)});
}
inline sf::Vector2f getCenter(const sf::Sprite& entity)
{
return sf::Vector2f{entity.getGlobalBounds().width / 2, entity.getGlobalBounds().height / 2} + entity.getPosition();
}
inline void scaleTo(sf::Sprite& entity, const sf::Vector2f& size)
{
entity.scale(size.x / entity.getGlobalBounds().width, size.y / entity.getGlobalBounds().height);
return;
}
inline void scaleToWidth(sf::Sprite& entity, const float &width)
{
entity.scale(width / entity.getGlobalBounds().width, width / entity.getGlobalBounds().width);
return;
}
inline void scaleToHeight(sf::Sprite& entity, const float &height)
{
entity.scale(height / entity.getGlobalBounds().height, height / entity.getGlobalBounds().height);
return;
}
#endif
Edit: it works with the inline keyword