C++ 如何在类中传递函数作为参数?

C++ 如何在类中传递函数作为参数?,c++,class,c++11,templates,member-function-pointers,C++,Class,C++11,Templates,Member Function Pointers,我正试图将我的print函数作为参数传递给属于模板化的binarySearchTree的inordertraversal类。每当我在main中定义这些函数时,程序都可以正常工作,但每当我尝试封装它们时,就会出现错误: "expected primary-expression before '&' token" 这就是有效的代码 void print(classdatatype& x); int main() { binarySearchTree<classdat

我正试图将我的
print
函数作为参数传递给属于模板化的
binarySearchTree
inordertraversal
类。每当我在main中定义这些函数时,程序都可以正常工作,但每当我尝试封装它们时,就会出现错误:

"expected primary-expression before '&' token"
这就是有效的代码

void print(classdatatype& x); 
int main()
{
    binarySearchTree<classdatatype> tree;
    tree.inorderTraversal(print);
    return 0;
}

void print(classdatatype& x)   {  cout << x << " ";      } 
如果需要,我可以显示其余的代码,但这一切都很好

一旦我把这些函数移到我的类中,它看起来是这样的 (在.cpp中,
print
binarySearchTree
的声明与上面声明的相同)

windlogtype

#include "windlogtype.h"
windlogtype::windlogtype() { }    
windlogtype::windlogtype(int i)     {   num = i; }    
int windlogtype::Getnumber() const  {   return num; }    
void windlogtype::Setnumber(int i)  {   num = i; }    
ostream operator<<(ostream& os, const windlogtype& w)
{
    os << w.Getnumber() << '\n';
    return os;
}

#ifndef WINDLOGTYPE_H
#define WINDLOGTYPE_H

#include <iostream>
using namespace std;

class windlogtype
{
public:
    windlogtype();
    windlogtype(int i);
    int Getnumber() const;
    void Setnumber(int i);
private:
    int num;
};
ostream operator<<(ostream& os, const windlogtype& w);

#endif // WINDLOGTYPE_H
#include <iostream>
#include <assert.h>
using namespace std;

template <class elemType> struct binaryTreeNode
{
    elemType info;
    binaryTreeNode<elemType>* llink;
    binaryTreeNode<elemType>* rlink;
};

template <class elemType> class binarySearchTree
{
public:
    const binarySearchTree<elemType>& operator=(const binarySearchTree<elemType>&);
    void inorderTraversal(void (*visit) (elemType&));
    binarySearchTree();
    ~binarySearchTree();
    binaryTreeNode<elemType>* root;
private:
    void inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType&));
};

template <class elemType> binarySearchTree<elemType>::binarySearchTree() {
    root = NULL;
}    
template <class elemType> void binarySearchTree<elemType>::inorderTraversal(void (*visit) (elemType& item))
{
    inorder(root, *visit);
}    
template <class elemType> void binarySearchTree<elemType>::inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType& item))
{
    if (p != NULL)
    {
        inorder(p->llink, *visit);
        (*visit)(p->info);
        inorder(p->rlink, *visit);
    }
}
#ifndef BST_H
#define BST_H
#include "binarySearchTree.h"
#include "windlogtype.h"
using namespace std;

class bst
{
public:
    bst();
    void InsertTree(windlogtype& newwind);
    void printfunctions(windlogtype& x);
    binarySearchTree<windlogtype>& GetTree();
    void print(windlogtype& x);
private:
    binarySearchTree<windlogtype> treeRoot;
};    
#endif // BST_H

#include "bst.h"

bst::bst(){/*ctor*/ }    
binarySearchTree<windlogtype>& bst::GetTree() {     return treeRoot;  }
void bst::print(windlogtype& x)               {     cout << x << " ";  }
void bst::printfunctions(windlogtype& x)
{
    treeRoot.inorderTraversal(print(windlogtype & x)); // error lies here
}
课程
bst

#include "windlogtype.h"
windlogtype::windlogtype() { }    
windlogtype::windlogtype(int i)     {   num = i; }    
int windlogtype::Getnumber() const  {   return num; }    
void windlogtype::Setnumber(int i)  {   num = i; }    
ostream operator<<(ostream& os, const windlogtype& w)
{
    os << w.Getnumber() << '\n';
    return os;
}

#ifndef WINDLOGTYPE_H
#define WINDLOGTYPE_H

#include <iostream>
using namespace std;

class windlogtype
{
public:
    windlogtype();
    windlogtype(int i);
    int Getnumber() const;
    void Setnumber(int i);
private:
    int num;
};
ostream operator<<(ostream& os, const windlogtype& w);

#endif // WINDLOGTYPE_H
#include <iostream>
#include <assert.h>
using namespace std;

template <class elemType> struct binaryTreeNode
{
    elemType info;
    binaryTreeNode<elemType>* llink;
    binaryTreeNode<elemType>* rlink;
};

template <class elemType> class binarySearchTree
{
public:
    const binarySearchTree<elemType>& operator=(const binarySearchTree<elemType>&);
    void inorderTraversal(void (*visit) (elemType&));
    binarySearchTree();
    ~binarySearchTree();
    binaryTreeNode<elemType>* root;
private:
    void inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType&));
};

template <class elemType> binarySearchTree<elemType>::binarySearchTree() {
    root = NULL;
}    
template <class elemType> void binarySearchTree<elemType>::inorderTraversal(void (*visit) (elemType& item))
{
    inorder(root, *visit);
}    
template <class elemType> void binarySearchTree<elemType>::inorder(binaryTreeNode<elemType>* p, void (*visit) (elemType& item))
{
    if (p != NULL)
    {
        inorder(p->llink, *visit);
        (*visit)(p->info);
        inorder(p->rlink, *visit);
    }
}
#ifndef BST_H
#define BST_H
#include "binarySearchTree.h"
#include "windlogtype.h"
using namespace std;

class bst
{
public:
    bst();
    void InsertTree(windlogtype& newwind);
    void printfunctions(windlogtype& x);
    binarySearchTree<windlogtype>& GetTree();
    void print(windlogtype& x);
private:
    binarySearchTree<windlogtype> treeRoot;
};    
#endif // BST_H

#include "bst.h"

bst::bst(){/*ctor*/ }    
binarySearchTree<windlogtype>& bst::GetTree() {     return treeRoot;  }
void bst::print(windlogtype& x)               {     cout << x << " ";  }
void bst::printfunctions(windlogtype& x)
{
    treeRoot.inorderTraversal(print(windlogtype & x)); // error lies here
}
\ifndef BST\u H
#定义BST_H
#包括“binarySearchTree.h”
#包括“windlogtype.h”
使用名称空间std;
bst级
{
公众:
bst();
void InsertTree(windlogtype和newwind);
void打印函数(windlogtype&x);
二进制搜索树&GetTree();
作废打印(windlogtype&x);
私人:
二叉树;
};    
#endif//BST\u H
#包括“bst.h”
bst::bst(){/*ctor*/}
binarySearchTree&bst::GetTree(){return treeRoot;}
void bst::print(windlogtype&x){cout

因此,结果也将不同


OP在评论中提到,他/她希望将成员函数
print()
bst
类传递给成员函数
inorderTraversal()
binarySearchTree
类的
。在这种情况下,传递成员函数是不够的,除此之外,还应传递将调用
print
函数的类的实例

通过捕获
bst
类的实例并传递给
binarySearchTree
类的
inorderTraversal()
,该函数可以方便地简化此过程

这意味着,在
模板类binarySearchTree
中提供:

template<typename Callable>
void inorderTraversal(Callable visit)
{
    inorder(root, visit);  // simply pass visit further
    // or avoid coping by warapping std::cref(): i.e. inorder(root, std::cref(visit));
}

template<typename Callable>
void inorder(binaryTreeNode<elemType>* p, Callable visit)
{
    if (p != NULL)
    {
        inorder(p->llink, visit);  // or inorder(root, std::cref(visit));
        visit(p->info);            // call directly with parameter
        inorder(p->rlink, visit);  // or inorder(root, std::cref(visit));
    }
}
以下是编译代码:



< > >强> ps <强>:另一个错误是从<代码>操作程序中发现的,为什么你不使用函数代码<函数>代码>参数类型?我对C++是很新的,我不确定STD:函数是什么,或者如何使用它。当你把函数移到你的类时,你是否让它成为了代码>静态< /代码>?如果你提供了一个澄清:您是否试图将
print
bst
类传递到
binarySearchTree
类的
inorderTraversal
void print(classdatatype& x);    // is a free function.
template<typename Callable>
void inorderTraversal(Callable visit)
{
    inorder(root, visit);  // simply pass visit further
    // or avoid coping by warapping std::cref(): i.e. inorder(root, std::cref(visit));
}

template<typename Callable>
void inorder(binaryTreeNode<elemType>* p, Callable visit)
{
    if (p != NULL)
    {
        inorder(p->llink, visit);  // or inorder(root, std::cref(visit));
        visit(p->info);            // call directly with parameter
        inorder(p->rlink, visit);  // or inorder(root, std::cref(visit));
    }
}
void printfunctions(windlogtype& x)
{
    // lambda captures the instance by copy
    const auto printThisLogType = [this](windlogtype & x)->void { this->print(x); };
    treeRoot.inorderTraversal(printThisLogType); // pass the callable lambda
}