class Test
{
public:
int maxData(int x,int y)
{
return (x>y)?x:y;
}
int process(int x,int y,int(* fun)(int,int))
{
return (* fun)(x,y);
}
void mainTest()
{
int result = process(1,2,maxData);
printf("%d",result);
}
};void main()
{
Test t;
t.mainTest();
}
编译出错,Test.cpp(16) : error C2664: 'process' : cannot convert parameter 3 from 'int (int,int)' to 'int (__cdecl *)(int,int)'
        None of the functions with this name in scope match the target type
为什么呢?
应该怎么写才对呢?

解决方案 »

  1.   

    return (* fun)(x,y);
    返回的不是int 
    你想返回的是指针,还是int整型啊?
      

  2.   

    class Test 

    public: 
    static int maxData(int x,int y) 

    return (x>y)?x:y; 

    int process(int x,int y,int(* fun)(int,int)) 

    return fun(x,y); 

    void mainTest() 

    int result = process(1,2,maxData); 
    printf("%d",result); 

    }; 把maxData函数定义为静态函数即可.
      

  3.   

    哦,可以了。
    弱弱的在问一句,为什么声明static就可以了呢
      

  4.   


    我这样理解:声明为static后当构造类对象的时候就为这个函数分配了空间不是static不会分配