关于函数指针的问题!

omrhal 2002-12-02 06:25:57
本人写一个计算器的程序,在编“等号”的函数时在不同情况下要用到不同函数,比方说先“加”运算之后按等号要一直加,先“减”后要一直减,所以想用函数指针来调用不同函数,部分程序如下,但函数指针部分老通不过,不知是不是在类里的原因。哪位仁兄给改一下,再给个用“函数指针数组”的例子。多谢!
#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)
...全文
25 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
omrhal 2002-12-05
  • 打赏
  • 举报
回复
非常感谢!无以为报,把分给你吧!
dsangvei 2002-12-02
  • 打赏
  • 举报
回复
(this->*func_ptr[choice])();////!!!!!!!!!!!!!!
xunknown(爱你是我一生中的理想) 说得没有错。这是类的函数指针用法
xunknown 2002-12-02
  • 打赏
  • 举报
回复
#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();

};
typedef void (Computer::*PF)(void);
////void (*func_ptr[2])()={Computer::add,Computer::sum};////ERROR
PF 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;
(this->*func_ptr[choice])();////!!!!!!!!!!!!!!
};
void main()
{Computer V;
V.init();
V.add();
V.mr();
V.init();
V.mp();
V.sum();
}
///////////////////
指向类成员函数的指针起定义和一般的函数指针有区别的。
C++有两个运算符->* and .*(这是C没有的)就是拿来处理指向类成员函数或者变量的运算符。
你的函数指针数组格式没有错。只是指针类型错了:需要指明是指向哪一类的成员函数。
如果有MSDN,可以看看详细的说明。如果没有,你也可以看看MFC的消息宏的定义!

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧