std::p tr_fun 模板化类和结构的参数困难
std::ptr_fun Arguments difficulty with templated class and struct
我正在尝试使用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;
恐怕我无法在这里测试代码。
相关文章:
- 使用不带参数的函数访问结构元素
- 将结构字段的类型展开为可变模板参数
- MSVC将仅移动结构参数解释为指针
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 如何传递带有通过引用传递的结构参数的函数?
- 展开可变参数模板结构
- 在类构造函数中定义结构变量的参数
- 如何在方法中传递结构参数
- 我应该在C++中将这些结构用作参数化构造函数吗?
- 在模板方法 c++ 中传递结构参数
- 结构参数在函数调用时损坏
- 如何使用结构参数委托构造函数
- 错误 C2783:无法推断结构参数的模板参数
- 用于存储元组键的数据结构:参数关系列表
- 使用结构参数从程序集中调用C++函数
- 如何为llvm IR调用指令创建结构参数
- c++为函数使用结构参数而不是多个参数
- 导入结构参数
- 将结构参数传递给结构内的函数
- 带有结构参数的pthread不起作用