I'm searching to optimize an error of approximation using the dlib library... so let's say I have Point (x,y) and a vector of values, which are used to find the minimum and fit the error locally, so I implemented this class:
#include <iostream>
#include <vector>
#include <cmath>
#include <dlib/optimization.h>
class Point
{
int _x, _y;
public:
Point(int x, int y): _x(x), _y(y){}
double findError(const double& psi)
{
auto err = std::erf(_x - psi) - std::erf(_y-psi);
return std::pow(err,2);
}
double optimize(const Point& p, dlib::matrix<double,0,1> &psiValues)
{
auto err = p->*findError ;
dlib::find_min_box_constrained(bfgs_search_strategy(),
objective_delta_stop_strategy(1e-9),
findError, derivative(findError), psiValues, {0.1,0.5,0.9},{0.1,0.4,0.8 });
return psiValues(0);
}
};
and this code don't compile so I tried to extract the optimizer in a static class like this:
#include <iostream>
#include <vector>
#include <cmath>
//#include <dlib/optimization.h>
class Point
{
int _x, _y;
public:
Point(int x, int y): _x(x), _y(y){}
double findError(const double& psi)
{
auto err = std::erf(_x - psi) - std::erf(_y-psi);
return std::pow(err,2);
}
};
class Errorcalculator
{
public:
static double optimize(const Point& p, std::vector<double> &psiValues)
{
auto err = p->*findError ;
// dlib::find_min_box_constrained(bfgs_search_strategy(),
// objective_delta_stop_strategy(1e-9),
// p->*findError, derivative(p->*findError), psiValue, {0.1,0.9 },{0.1 0.8});
return 0.0;
}
};
now I'm getting (the same compile error using dlib function with or without static keyword)
error: 'findError' was not declared in this scope
How is it possible if the point p is declared as parameter? How can I use the dlib function wrapped in the class?
findError is member method, but isn't pointer. For creating pointer to method use next syntax: return value (class_name::* pointer_name)(method arguments); Sample:
class widget
{
public:
unsigned char foo(long long, double) {}
};
int main()
{
// declaration of pointer
unsigned char (widget::*ptr_to_method)(long long, double);
// setting the pointer on certain method in class (not object!)
ptr_to_method = &widget::foo;
// we have some object of widget class
widget obj{};
// using the pointer
unsigned char res = (obj.*ptr_to_method)(10L, 10.0); // arguments
// if we have obj as widget*, we'll use ->* syntax to call method
}
In this sample we used local pointer, may be you would to use global pointer. Read more, for example, here