请各位赐教:函数指针数组的有关问题,实在不明白为什么有错?
请各位赐教:函数指针数组的问题,实在不明白为什么有错??
#include <iostream>
using namespace std;
class A
{
public:
void f(int)
{
cout<<"a";
}
void f(double)
{
cout<<"b";
}
void f2(int)
{
cout<<"c";
}
void f2(double)
{
cout<<"d";
}
};
int main(int argc, char* argv[])
{
A a;
void (*f_c[2])(int) = {a.f, a.f2};
f_c[0](0);
return 0;
}
vs提示错误:
a pointer to a bound function may only be used to call the function
编译报错:
error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(int)'
还有个问题:
f_c[0](0);
还可以这样写: (*f_c[0])(0); ,这是为什么呢?
------解决方案--------------------
仅供参考
#include <iostream>
using namespace std;
class A
{
public:
void f(int)
{
cout<<"a";
}
void f(double)
{
cout<<"b";
}
void f2(int)
{
cout<<"c";
}
void f2(double)
{
cout<<"d";
}
};
int main(int argc, char* argv[])
{
A a;
void (*f_c[2])(int) = {a.f, a.f2};
f_c[0](0);
return 0;
}
vs提示错误:
a pointer to a bound function may only be used to call the function
编译报错:
error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(int)'
还有个问题:
f_c[0](0);
还可以这样写: (*f_c[0])(0); ,这是为什么呢?
------解决方案--------------------
仅供参考
- C/C++ code
#include <stdio.h> double A[300][100]; double valuex[300]; double valuey[300]; int i; double fun00(double x,double y) {return x + y ;}; double fun01(double x,double y) {return x*x+ 3*y ;}; double fun02(double x,double y) {return x + 2*y*x;}; //... fun03(double x,double y) {return ...+... ;}; //... //... fun98(double x,double y) {return ...+... ;}; double fun99(double x,double y) {return x/2+20*y ;}; double (*funNN[100])(double,double)={ fun00, fun01, fun02, // fun03, // ... // fun98, fun99, }; int main() { double x,y; int f,FN; for (i=0;i<300;i++) { valuex[i]=(double)i; valuey[i]=(double)i; } FN=3; for (i=0;i<300;i++) { x=valuex[i]; y=valuey[i]; for (f=0;f<FN;f++) A[i][f]=funNN[f](x,y); } for (i=0;i<3;i++) { for (f=0;f<FN;f++) printf("%lg ",A[i][f]); printf("\n"); } return 0; } //0 0 0 //2 4 3 //4 10 10
------解决方案--------------------
你他呀的用的类函数, 没看到的.
typedef void (A::*ptr_func)(int);
ptr_func f_c[2];