g++ linker can't find const member function
- by Max
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.