我有以下代码
#include<iostream>
using namespace std;
class operate
{
int x;
int y;
public:
operate(int _x, int _y):x(_x), y(_y)
{}
void add(const char* ch)
{
cout<<ch<<" "<<x+y;
}
void subtract(const char* ch)
{
cout<<ch<<" "<<x-y;
}
void multiply(const char* ch)
{
cout<<ch<<" "<<x*y;
}
};
int main()
{
void (operate::*fptr[3])(const char*);
operate obj(2,3);
fptr[0] = &(operate.add); //problem
fptr[1] = &(operate.multiply); //problem
fptr[2] = &(operate.subtract); //problem
(obj.*fptr[0])("adding");
(obj.*fptr[1])("multiplying");
(obj.*fptr[2])("subtracting");
}
看来我没有正确地将成员函数分配给函数指针数组。我该如何解决这个问题。我正在使用 VS2010