std::ptr_fun 模板化 class 和结构的参数困难

std::ptr_fun Arguments difficulty with templated class and struct

我正在尝试通过使用 libkdtree++ 来实现我的方式,尝试实现 RRT,尽管我发现在理解如何使用这个库时遇到了一些困难。在 examples 之后,我尝试这样定义我的 RRT class 的轮廓:

#pragma once

#include "coupling_tree.h"
#include "kdtree++/kdtree.hpp"
#include <deque>
#include <iostream>
#include <vector>
#include <limits>
#include <functional>
#include <set>


namespace trajectory {


    template<int c_dim> struct Point {

        std::set<const void*> registered;
        Eigen::VectorXd p;


        Point(Eigen::VectorXd point) :
            p(point)
        {
            assert(point.size() == c_dim);
        }



        ~Point()
        {
            bool unreg_ok = (registered.find(this) != registered.end());
            assert(unreg_ok);
            registered.erase(this);
        }


        double distance_to(Point const & x) const
        {
            double dist = 0;
            for (int i = 0; i < c_dim; ++i)
                dist += (p[i] - x[i])*(p[i] - x[i]);
            return std::sqrt(dist);
        }

        inline double operator[](size_t const N) const { return p(N); }



    };

    template<int c_dim> double tac(Point<c_dim> p, size_t k) { return p[k]; }


    template<int plant_dim, int c_dim>
    class RRT { //TODO: Should this be abstract so we can quickly implement lots of RRT variants?

        ///////TYPEDEFS
        typedef Point<c_dim> point;
        typedef KDTree::KDTree<c_dim, point, std::pointer_to_binary_function<point, size_t, double> > kd_tree;



        ////////////VARIABLES

    private:
        kd_tree tree;


        ////////////////////




    public:





    protected:


    private:

        const int getNumDim() const {
            return plant_dim;
        }


    };



}

这会产生以下错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'std::pointer_to_unary_function<trajectory::Point<5>,std::size_t,size_t (__cdecl *)(trajectory::Point<5>)> std::ptr_fun<trajectory::Point<5>,std::size_t>(_Result (__cdecl *)(_Arg))': cannot convert argument 1 from 'double (__cdecl *)(trajectory::Point<c_dim>,std::size_t)' to 'size_t (__cdecl *)(trajectory::Point<5>)'  test_RRT    C:\ResearchCode\robot-new\robot\projects\RRT\include\RRT.h  83  
Error   C2512   'std::pointer_to_binary_function<trajectory::Point<5>,std::size_t,double,_Result (__cdecl *)(_Arg1,std::_Arg2)>': no appropriate default constructor available  test_RRT    C:\ResearchCode\robot-new\robot\externals\libkdtree\kdtree++\kdtree.hpp 126 

我对这里的输入和投诉的具体内容感到迷茫,特别是因为我是 ptr_fun 以这种方式使用的新手。有人可以解释错误和修复吗?

所以,您的代码不完整,因为它缺少导致触发器的重要实现。看警告

cannot convert argument 1 from 'double (__cdecl *)(trajectory::Point<c_dim>,std::size_t)' to 'size_t (__cdecl *)(trajectory::Point<5>)'

所以您正在尝试调用需要 size_t 作为双精度参数的东西。

我能想到的一件事是std::pointer_to_binary_function<point, size_t, double>。例如,如果您将此用于 double distance_to(Point const & x),它将不起作用,因为距离需要两个 Point-type 输入。

编辑: 所以看行

typedef KDTree::KDTree<c_dim, point, std::pointer_to_binary_function<point, size_t, double> > kd_tree;

我看不到你在哪里使用这个类型,但是这个类型本身是基于一个模板化的类型,它有三个参数。我不确定期望的是什么,但是您将模板参数 1 设置为 c_dim,将参数 2) 设置为 Point,将参数 3 设置为...未定义的二进制函数指针?!

您应该 look-up 有关 std::pointer_to_binary_function 的更多信息,因为您将看到您需要为构造函数提供您希望指向的函数。 IE。

typedef KDTree::KDTree<c_dim, point, std::pointer_to_binary_function<point, size_t, double>(tac) > kd_tree;

恐怕我不能在这里测试代码。