0
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();
}

I want to pass a friend function pointer to member function.

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.
Pang
  • 9,564
  • 146
  • 81
  • 122
Ruoxuan.Wang
  • 179
  • 1
  • 2
  • 13

1 Answers1

1
  • you've declared non-template function visitNode_ as a friend, you should properly forward declare it and then declare as a template friend:
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_ is actually a function template and you didn't provide list of template parameters when taking it's address
ptr = &visitNode_< T >;
user7860670
  • 35,849
  • 4
  • 58
  • 84