HI,大家好!
我正在学数据结构,写2叉树遍历。
遍历函数PreOrder,InOrde,PostOrder都写好了,测试了也都有用。
然后要求写个统计叶节点的函数
template <class T>
class MyBinaryTree()
{
    int TreeSize()
   {
m_treeSize=0;
PostOrder(TreeSize_1,root);
return m_treeSize;
    }
    void TreeSize_1(MyBinaryTreeNode<T>* t)
   {
if(p->pLeft==0&&p->pRight==0) treeSize++;
    }
   int m_treeSize;
}
结果
Error 1 error C3867: 'MyBinaryTree<T>::TreeSize_1': function call missing argument list; use '&MyBinaryTree<T>::TreeSize_1' to create a pointer to member d:\c++ develop\dsaa\mybinarytree\main\mybinarytree.h 72
我按照它的提示修改 仍然没用
我去网上找 说要改成静态函数或全局函数 因为修改了m_treeSize 所以明显没办法用静态函数 如果改成全局函数 又破坏了类的完整性
有什么好的解决办法吗?
谢谢各位了

解决方案 »

  1.   

    没看明白你的代码,
    不过好像是要在类里定义函数指针为成员变量,那就看着下面关于函数指针代理的例子改吧:
    #include <iostream>
    #include <stdlib.h>using namespace std;//*
    template<class T>
    class A
    {
    public: typedef int (T::*delegateFun)(int); A(T* c, delegateFun f)
    : _c(c), _f(f)
    {
    } int execute(int i)
    {
    return (_c->*_f)(i);
    }private: T* _c;
    delegateFun _f;
    };class B
    {
    public: B()
    {
    } int incFunc(int i)
    {
    return ++i;
    } int decFunc(int i)
    {
    return --i;
    }
    };void main()
    {
    B* pB = new B;
    A<B> c1(pB, &B::incFunc);
    A<B> c2(pB, &B::decFunc); cout<<c1.execute(10)<<endl;
    cout<<c2.execute(10)<<endl; delete pB; system("pause");
    }