I am trying to pass a function pointer to a member function of a class to std::unique() and am getting some compiler errors that are impossible for me to decipher.
// So this will otherwise compile
class Vec2 {};
class Polygon {};
// ConvexHull.h:
#include <vector>
#include <algorithm>
class ConvexHull {
public:
// Used to sort an array of vectors so the bottom-right element is first:
bool bottomRight(const Vec2& a, const Vec2& b);
// Used to determine if coordinates are equivalent:
bool vecEqual(const Vec2& a, const Vec2& b);
Polygon chGrahamScan(std::vector<Vec2> points);
};
// ConvexHull.cpp:
bool ConvexHull::bottomRight(const Vec2& a, const Vec2& b) {
// implementation...
return false; // So this will otherwise compile
}
bool ConvexHull::vecEqual(const Vec2& a, const Vec2& b) {
// implementation...
return false; // So this will otherwise compile
}
Polygon ConvexHull::chGrahamScan(std::vector<Vec2> points) {
// Sort according to bottom right:
std::sort(points.begin(),points.end(),bottomRight); // !!!Does not compile!!!
std::vector<Vec2>::iterator it;
// Get rid of duplicate points:
it = std::unique(points.begin(),points.end(),vecEqual); // !!!Does not compile!!!
// rest of implementation...
Polygon poly; // So this will otherwise compile
return poly;
}
int main(){return 0;} // again, to allow this to otherwise compile
Ignoring the two broken lines, this is enough to compile something. Please help! I am not sure what I am doing wrong here. I know this test code says the members functions are public but my end goal here is to have them be private functions that are hidden from the user but still used internally in ConvexHull::chGrahamScan() by sort and unique. These aren't the only two comparison methods I intend to use. The others require referring to internal state data maintained by an instance of the ConvexHull class. My initial solution (which apparently totally worked) had methods that did not belong to any class. Instead all of the state data and comparison functions were in a global anonymous namespace, but this broke the thread-safeness of my project so I decided to go this route.
If you copy and paste this into an empty project you should see the errors I am getting. It says I need to use .* or ->* for a member function pointer. However trying to do this gives me other errors, saying these functions cannot be used as member functions as they are of type "unresolved overloaded function".
If it turns out that I cannot do something like this, how would I best go about implementing std::sort and std::unique in such a way that the comparison functions passed to them use state data external to the definition of the functions themselves without violating thread safety by using global variables? My previous solution did something like:
namespace {
Vec2 point0; // state data that other comparison functions like
// bottomRight and vecEqual need
bool bottomRight(/*...*/){/*...*/}
bool vecEqual(/*...*/){/*...*/}
}
// Accesses global data. Bad!
Polygon chGrahamScan(std::vector<Vec2> points) {
std::sort(points.begin(),points.end(),bottomRight);
std::vector<Vec2>::iterator it;
it = std::unique(points.begin(),points.end(),vecEqual);
// etc..
}