views:

69

answers:

1

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.

+2  A: 

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.

Mark B
I found the problem. The object file of the calling file was still looking for the old non-const version, and make didn't update it, since none of the code for that object file was changed. Recompiling from scratch fixed it. Thanks for your help, everyone.
Max