关于函数指针的问题!
本人写一个计算器的程序,在编“等号”的函数时在不同情况下要用到不同函数,比方说先“加”运算之后按等号要一直加,先“减”后要一直减,所以想用函数指针来调用不同函数,部分程序如下,但函数指针部分老通不过,不知是不是在类里的原因。哪位仁兄给改一下,再给个用“函数指针数组”的例子。多谢!
#include<iostream.h>
#include<math.h>
class Computer
{
private:
double f_value;
double n_value;
double e_value;
double r_value;
int choice;
double input()
{
double v;
cout<<"please input a value:"<<endl;
cin>>v;
return v;
}
public:
Computer()
{f_value=0;
n_value=0;
e_value=0;
r_value=0;}
void init()
{f_value=input();}
void mc()
{r_value=0;}
void ms()
{f_value=r_value;
cout<<r_value;}
void mr()
{r_value=f_value;}
void mp()
{ r_value=r_value+f_value;}
void add();
void sum();
void equel();
};
void (*func_ptr[2])()={Computer::add,Computer::sum};
void Computer::add()
{cout<<f_value;
n_value=input();
f_value=f_value+n_value;
choice=0;
};
void Computer::sum()
{cout<<f_value;
n_value=input();
f_value=f_value-n_value;
choice=1;
};
void Computer::equel()
{e_value=f_value;
cout<<e_value<<endl;
(*func_ptr[choice])();
};
void main()
{Computer V;
V.init();
V.add();
V.mr();
V.init();
V.mp();
V.sum();
}
///////////////////////////////
Vc环境下编译结果:
计算器.cpp
e:\计算器.cpp(47) : error C2440: 'initializing' : cannot convert from 'void (__thiscall Computer::*)(void)' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible
e:\计算器.cpp(47) : error C2440: 'initializing' : cannot convert from 'void (__thiscall Computer::*)(void)' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible
Error executing cl.exe.
计算器.exe - 2 error(s), 0 warning(s)