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

标签 c++ g++ constants linker-errors

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

bool withinBounds(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: 未定义对 'yarl::utility::Point::withinBounds(yarl::utility::Point const& , yarl::utility::Point const&)'

当我使函数不是 const 时,它链接正常。任何人都知道为什么?链接器错误看起来像是在寻找函数的非 const 版本,但我不知道为什么会这样。

最佳答案

看起来在调用文件中,无论包含什么 header 以给出 withinBounds 的声明,都有不正确的声明,该方法的非常量版本(请注意, undefined reference 是一个非常量版本)。确保您的项目包含目录不包含多个版本的 header 。还要确保您确实按预期更改并保存了 header 。

关于c++ - g++ 链接器找不到 const 成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3018511/

相关文章:

c++ - Protocol Buffer 和实际传输选项 - 套接字或中间件

c++ - 不必要命名空间的编译器警告

c++ - 奇怪的错误,set<int>::begin() 总是返回 const 迭代器

c++ - 我应该制作所有不应该改变常量的东西吗?

c++ - 延长临时工的生命周期

ruby-on-rails - ruby on rails,变量后面或前面的冒号

C++11 枚举转发导致 "underlying type mismatch"

c++ - 小数据传输时套接字发送/接收返回 0

c++ - LLVM:使用空指针操作数创建 CallInst

c++ - 使用 Linux 命令行 g++ 构建 Crypto++ 5.6.5 问题