2013-04-04 49 views
2

有人可以帮助我确定哪里出错了吗?我想使用函数指针的基类功能指向基类错误的成员函数

错误C2064:术语不计算为取0参数上线人数30即*(APP)()

#include<stdio.h> 
#include<conio.h> 
#include<stdarg.h> 
#include<typeinfo> 

using namespace std; 

class A{ 
public: 
    int a(){ 
     printf("A"); 
     return 0; 
    } 
}; 

class B : public A{ 
public: 
    int b(){ 
     printf("B"); 
     return 0; 
    } 
}; 

class C : public B{ 
public: 
    int(C::*app)(); 
    int c(){ 
     app =&C::a; 
     printf("%s",typeid(app).name()); 
     *(app)(); 
     printf("C"); 
     return 0; 
    } 
}; 
int main(){ 
    C *obj = new C(); 
    obj->c(); 
    getch(); 
} 
+2

尝试'(本 - > *应用程序)();' – WhozCraig 2013-04-04 07:06:42

+1

为什么你试图用一个指针的基类的功能?有没有什么特别的原因,你不能只调用'A :: a();'? – maditya 2013-04-04 07:08:47

+0

@WhozCraig非常感谢!知道了 – 2013-04-04 07:11:24

回答

2

指针的函数成员必须始终与对象的指针/引用结合使用。你大概意思写:

(this->*app)(); 
+0

非常感谢!它的工作原理 – 2013-04-04 09:59:18

3

你必须使用*或 - > *在调用指向成员函数

class C : public B{ 
    public: 
     int(C::*app)(); 
     int c(){ 
      app =&C::a; 
      printf("%s",typeid(app).name()); 
      (this->*app)(); // use ->* operator within() 
      printf("C"); 
      return 0; 
     } 
    }; 
0

我有一种预感,你真的是在努力解决这个问题。是“如何从派生类调用基类函数?”如果是这样的话,答案是:

int c(){ 
    app =&C::a; 
    printf("%s",typeid(app).name()); 
    A::a(); //A:: scopes the function that's being called 
    printf("C"); 
    return 0; 
} 

int c(){ 
    app =&C::a; 
    printf("%s",typeid(app).name()); 
    this->a(); 
    printf("C"); 
    return 0; 
} 
1

http://msdn.microsoft.com/en-us/library/k8336763.aspx

的二进制运算符 - > *结合了其第一个操作数,它必须是一个 指向一个类类型的对象,第二个操作数,其中 必须是指向成员类型的指针。

因此,这将解决您的问题:

class C : public B { 
    public: 
     int(C::*app)(); 

     int c() { 
      app = &C::a; 
      printf("%s", typeid(app).name()); 
      (this->*app)(); 
      printf("C"); 
      return 0; 
     } 
}; 
+0

对不起,当我搜索并发布答案时,其他人已经比我快。我必须知道我处于stackoverflow的世界! – 2013-04-04 07:24:20