简体   繁体   English

std :: ptr_fun模板化类和结构的参数难度

[英]std::ptr_fun Arguments difficulty with templated class and struct

I'm trying to work my way through using libkdtree++, trying to implement RRT, though I'm finding some trouble understanding how to use this library. 我在尝试使用libkdtree ++的过程中尝试实现RRT,尽管在理解如何使用此库方面遇到了一些麻烦。 Following the examples , I try to define the outline of my RRT class as such: 按照示例 ,我尝试这样定义RRT类的轮廓:

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


    };



}

This yields the following errors: 这将产生以下错误:

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 

I'm getting very lost in the typing here and what the complaint specifically is, especially since I'm new to ptr_fun being used in this way. 我对这里的输入以及具体的抱怨感到迷失,尤其是因为我是ptr_fun的新手。 Can someone explain the error, and the fix? 有人可以解释错误和解决方法吗?

So, you're code is incomplete, as it's missing the vital implementation that causes the trigger. 因此,您的代码不完整,因为它缺少导致触发器的重要实现。 Look at the warning 看警告

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

So you're trying to call something that expects a size_t as an argument with a double. 因此,您正在尝试调用一个将size_t作为双精度参数的东西。

One of the things I can think of is std::pointer_to_binary_function<point, size_t, double> . 我能想到的一件事是std::pointer_to_binary_function<point, size_t, double> If you use this for double distance_to(Point const & x) it will not work for instance, as distance expects two Point-type inputs. 如果将此值用于double distance_to(Point const & x)则它将不起作用,因为distance需要两个Point类型的输入。

edit: So look at the line 编辑:所以看这行

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

I cannot see where you use this type, but the type itself is based on a templated type, that has three parameters. 我看不到您在哪里使用此类型,但类型本身基于具有三个参数的模板化类型。 I am not sure what is expected, but you set template argument 1 to c_dim, argument 2) to Point and argument 3 to... an undefined binary function pointer?! 我不确定会发生什么,但是您将模板参数1设置为c_dim,参数2)设置为Point,参数3设置为...未定义的二进制函数指针?

You should look-up more information on std::pointer_to_binary_function, as you will see that you need to supply the constructor with the function you wish to point to. 您应该在std :: pointer_to_binary_function上查找更多信息,因为您将看到需要为构造函数提供希望指向的函数。 Ie

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

I cannot test the code here, I'm afraid. 恐怕我无法在此处测试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM