我正在创建一个小的‘通用’寻路类,它接受一个类类型的Board,它将在这个类上寻找路径,
//T - Board class type
template<class T>
class PathFinder
{...}而Board也被模板化以保存节点类型。(这样我就可以在2D或3D矢量空间上找到路径)。
我希望能够为PathFinder声明和定义一个成员函数,它将像这样接受参数
//T - Board class type
PathFinder<T>::getPath( nodeType from, nodeType to);如何对作为参数传入函数的T和nodeType节点类型进行类型兼容?
发布于 2012-11-20 22:38:03
如果我知道你想要什么,给board一个类型成员并使用它:
template<class nodeType>
class board {
public:
typedef nodeType node_type;
// ...
};
PathFinder<T>::getPath(typename T::node_type from, typename T::node_type to);如果不能更改board,也可以对其进行模式匹配
template<class Board>
struct get_node_type;
template<class T>
struct get_node_type<board<T> > {
typedef T type;
};
PathFinder<T>::getPath(typename get_node_type<T>::type from, typename get_node_type<T>::type to);发布于 2012-11-20 22:37:53
您可以在类定义中typedef nodeType:
typedef typename T::nodeType TNode;
PathFinder<T>::getPath( TNode from, TNode to);https://stackoverflow.com/questions/13475439
复制相似问题