class Btree{
friend void visitNode_(BtreeNode<T>* node);
void DFSshow();
void showNode_(BtreeNode<T>* node,int step,void (*func)(BtreeNode<T>*));
}
template <class T>
void Btree<T>::DFSshow() {
void (*ptr)(BtreeNode<T>*);
ptr = &visitNode_;
this->showNode_(root,0,ptr);
}
template<class T>
void visitNode_(BtreeNode<T> *node) {
node->showNode();
}
我想将一个友元函数指针传递给成员函数。
errors:In file included from /Users/wangruoxuan/ClionProjects/btree/main.cpp:2:
/Users/wangruoxuan/ClionProjects/btree/Btree.hpp:157:12: error: use of undeclared identifier 'visitNode_'
ptr = &visitNode_;
^
1 error generated.
答案 0 :(得分:1)
visitNode_
声明为朋友,您应该正确转发声明它然后声明为模板朋友:template< class T > class
Btree;
template< class T > void
visitNode_(BtreeNode< T > * node);
template< class T > class
Btree
{
template< class T_ > friend void
visitNode_(BtreeNode< T_ > * node);
visitNode_
实际上是一个功能模板,在提取模板参数时,您没有提供模板参数列表ptr = &visitNode_< T >;