1

I'm building a program, and need a function to check if the mouse is on an object. I wrote the function and created a new .hpp-File, because multiple Files in my project will use the function. The File is called HitboxDetec.hpp. The function I wrote in it looks like this:

bool isClicked(sf::Sprite* Sprite, sf::RenderWindow* pW)
{
    int MOUSE_X = sf::Mouse::getPosition(*pW).x;
    int MOUSE_Y = sf::Mouse::getPosition(*pW).y;

    if (MOUSE_X >= Sprite->getPosition().x &&
        MOUSE_X <= Sprite->getPosition().x + Sprite->getPosition().x + 500 &&
        MOUSE_Y >= Sprite->getPosition().y &&
        MOUSE_Y <= Sprite->getPosition().y + Sprite->getPosition().y + 500)
    {
        return true;
    }
    else return false;
}

I included the needed libraries etc. and the code works (I tested it by declaring the function in my .cpp-File).

In my .cpp File I included the CookieDetec.hpp-File, and called it with

if(isClicked(pSprite, pW)) {Stuff}

When I compile my program, it just gives me a LNK2005-Error. How can I fix this?

D4RKS0UL23
  • 45
  • 1
  • 5

1 Answers1

3

LNK2005 is for:

The given symbol, displayed in its decorated form, was multiply defined.

if you defined your function in header file then it might be included in multiple translation units. To prevent multiple definitions mark this function as inline:

inline bool isClicked(sf::Sprite* Sprite, sf::RenderWindow* pW)
^^^^^^
marcinj
  • 48,511
  • 9
  • 79
  • 100