我正在搜索以使用dlib库优化近似误差...
因此,假设我有Point(x,y)和一个值向量,用于查找最小值并在本地拟合误差,因此我实现了此类:

#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);
    }
};


而且此代码无法编译,因此我尝试在静态类中提取优化器,如下所示:

#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;
    }
 };


现在我得到(使用带有或不带有静态关键字的dlib函数的相同编译错误)


  错误:在此范围内未声明“ findError”


如果将点p声明为参数怎么办?如何使用包装在类中的dlib函数?

最佳答案

findError是成员方法,但不是指针。
要创建指向方法的指针,请使用以下语法:
返回值(class_name :: * pointer_name)(方法参数);
样品:

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
}


在此示例中,我们使用了本地指针,也许您将使用全局指针。
阅读更多信息,例如,here

关于c++ - 在此范围内未声明使用dlib的成员函数指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51294567/

10-14 18:02
查看更多