【问题标题】:How to use pointer to friend function as a parameter of member function如何使用指向友元函数的指针作为成员函数的参数
【发布时间】:2017-04-22 06:41:14
【问题描述】:
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.

【问题讨论】:

    标签: c++ templates friend-function


    【解决方案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 >;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-09
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多