【问题标题】:LNK2005-Error when declaring a function in a .hpp-File [duplicate]LNK2005-在 .hpp 文件中声明函数时出错 [重复]
【发布时间】:2016-05-12 13:41:22
【问题描述】:

我正在构建一个程序,并且需要一个函数来检查鼠标是否在一个对象上。我编写了函数并创建了一个新的 .hpp-File,因为我的项目中的多个文件将使用该函数。该文件名为HitboxDetec.hpp。我在里面写的函数是这样的:

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;
}

我包含了所需的库等,并且代码有效(我通过在我的 .cpp 文件中声明函数来测试它)。

在我的 .cpp 文件中,我包含了CookieDetec.hpp-File,并用

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

当我编译我的程序时,它只会给我一个 LNK2005-Error。我该如何解决这个问题?

【问题讨论】:

    标签: c++ function sfml lnk2005


    【解决方案1】:

    LNK2005 适用于:

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

    如果您在头文件中定义了函数,那么它可能包含在多个翻译单元中。为防止多个定义将此函数标记为内联:

    inline bool isClicked(sf::Sprite* Sprite, sf::RenderWindow* pW)
    ^^^^^^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-03
      • 2017-07-14
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2010-11-08
      相关资源
      最近更新 更多