我有一个 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/