I am new to templating, but I am trying to make my primms algorithm work with two different distance calculators(thats what the functors are for) based on different constraints for a school project (they calculate the distance differently, one depends on if we care about terrain and the other does not). I declare like this
template <typename func>
double mst_mode(std::vector<primms_vertex> &mst_vec, const func &comp){
But later in the function when I attempt to use it like this:
if(i != working_vertex && !mst_vec[i].been_visited && mst_vec[i].distance > comp(mst_vec[working_vertex], mst_vec[i])){
I get the following errors :
No matching function for call to object of type 'const primms_vertex::euclids_distance' No matching function for call to object of type 'const primms_vertex::primms_distance'
One example of me calling the function is:
poke->mst_mode(poke->primms_vector, primms_vertex::primms_distance());
(the project is pokemon themed so excuse the funny class name) Both of the functors take in the exact same thing, 2 mst_vertex's, which is exactly what I am getting when I index into mst_vec. So I cannot figure out for the life of me what the compiler is complaining about. Any help would be appreciated!
Something like this:
#include <vector>
struct primms_vertex {
bool been_visited{};
size_t distance{};
static size_t primms_distance(primms_vertex const& left, primms_vertex const& right);
};
size_t primms_vertex::primms_distance(primms_vertex const& left, primms_vertex const& right) {
return left.distance < right.distance;
}
struct Poke {
std::vector<primms_vertex> primms_vector;
template <typename func>
double mst_mode(std::vector<primms_vertex>& mst_vec, const func& comp) {
size_t working_vertex{ 42 };
for (size_t i = 0; i < mst_vec.size(); i++) {
if (i != working_vertex && !mst_vec[i].been_visited && mst_vec[i].distance > comp(mst_vec[working_vertex], mst_vec[i])) {
// do something.
}
}
return 0.0;
}
};
int main() {
Poke* poke{}; // TODO: Instantiate a Poke.
poke->mst_mode(poke->primms_vector, primms_vertex::primms_distance);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.