std::p tr_fun 模板化类和结构的参数困难

std::ptr_fun Arguments difficulty with templated class and struct

本文关键字:结构 参数 tr fun std      更新时间:2023-10-16

我正在尝试使用libkdtree++,尝试实现RRT,尽管我发现在理解如何使用这个库时遇到了一些麻烦。 在示例之后,我尝试定义我的 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;
        }

    };

}

这将产生以下错误:

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:ResearchCoderobot-newrobotprojectsRRTincludeRRT.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:ResearchCoderobot-newrobotexternalslibkdtreekdtree++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)则它不起作用,因为距离需要两个点类型输入。

编辑:所以看线

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

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

您应该查找有关 std::p ointer_to_binary_function 的更多信息,因为您将看到您需要为构造函数提供您希望指向的函数。

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

恐怕我无法在这里测试代码。