如何使用指向友元函数的指针作为成员函数的参数
How to use pointer to friend function as a parameter of member function
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.
- 您已将非模板函数
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 >;
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.
- 您已将非模板函数
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 >;