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.
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.
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
精彩评论