g++链接器找不到 const 成员函数

发布于 2024-09-05 08:35:00 字数 1046 浏览 6 评论 0原文

我有一个 Point 类(带有整数成员 x 和 y),它有一个成员函数 withinBounds ,其声明如下:

bool insideBounds(const Point&, const Point&) const;bool insideBounds(const Point&, const Point&) const;

并这样定义:

bool Point::withinBounds(const Point& TL, const Point& BR) const
{
    if(x < TL.getX()) return false;
    if(x > BR.getX()) return false;
    if(y < TL.getY()) return false;
    if(y > BR.getY()) return false;

    // Success
    return true;
}

然后在另一个文件中,我像这样调用 withinBounds

Point pos = currentPlayer->getPosition();
if(pos.withinBounds(topleft, bottomright))
{
    // code block
}

这编译得很好,但无法链接。 g++ 给了我这个错误:

/home/max/Desktop/Development/YARL/yarl/src/GameData.cpp:61: undefined reference to 'yarl::utility::Point::withinBounds(yarl::utility: :Point const&, yarl::utility::Point const&)'

当我使函数不是 const 时,它链接得很好。有谁知道原因吗?链接器错误看起来像是在寻找该函数的非常量版本,但我不知道为什么会这样。

I have a Point class (with integer members x and y) that has a member function withinBounds that is declared like so:

bool withinBounds(const Point&, const Point&) const;

and defined like this:

bool Point::withinBounds(const Point& TL, const Point& BR) const
{
    if(x < TL.getX()) return false;
    if(x > BR.getX()) return false;
    if(y < TL.getY()) return false;
    if(y > BR.getY()) return false;

    // Success
    return true;
}

and then in another file, I call withinBounds like this:

Point pos = currentPlayer->getPosition();
if(pos.withinBounds(topleft, bottomright))
{
    // code block
}

This compiles fine, but it fails to link. g++ gives me this error:

/home/max/Desktop/Development/YARL/yarl/src/GameData.cpp:61: undefined reference to 'yarl::utility::Point::withinBounds(yarl::utility::Point const&, yarl::utility::Point const&)'

When I make the function not const, it links fine. Anyone know the reason why? The linker error looks like it's looking for a non-const version of the function, but I don't know why it would.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹短裙飘 2024-09-12 08:35:00

看起来在调用文件中,无论包含什么标头来给出 withinBounds 的声明,都具有该方法的非常量版本的不正确声明(请注意,未定义的引用是对非 -常量版本)。确保您的项目包含目录不包含标头的多个版本。还要确保您确实按照预期更改并保存了标题。

It looks like in the calling file, whatever header is included to give the declaration of withinBounds has the incorrect declaration, of a non-const version of the method (note that the undefined reference is to a non-const version). Make sure that your project include directories don't include multiple versions of the header. Also make sure that you did in fact change and save the header as you intended.

心如狂蝶 2024-09-12 08:35:00

我只想为未来的读者添加这里,因为这是谷歌上的接近结果,如果您确信您的定义是正确的,请尝试删除所有目标文件(.o),然后重新编译

I just want to add here for future readers since this is a close result on google, if you are confident that your definitions are correct, try deleting all of your object files (.o) and then recompiling

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文