函数的指针(按书上照葫芦画瓢都错)

napoleonpan1 2003-08-19 05:28:47
//比较大小
#include<iostream.h>
int max(int x,int y)
{
if(x>y)return x;
else return y;
}
void main()
{
int max(int,int);
int a,b,c;
int (*p)();
cin>>a>>b;
p=max;//出错显示cannot conver int(*)(int,int) to int(*)()
c=(*p)(a,b);
cout<<c;
}
...全文
32 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ehhl 2003-08-19
  • 打赏
  • 举报
回复
#include<iostream.h>


int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
void main()
{
//int max(int,int);此处可以不要
int a,b,c;
int (*p)(int,int);//此处声明如下
cin>>a>>b;
p=max;
c=(*p)(a,b);
cout<<c;
}
yellowwolf 2003-08-19
  • 打赏
  • 举报
回复
Wolf0403(完美废人)
能不能说说你那函数指针的用法,我看了书上说一般用于错误时回调的,VC里有的好多。
但自己没有用过,能不能给个例子出来看看啊。
非常谢了。
Wolf0403 2003-08-19
  • 打赏
  • 举报
回复
函数指针不多用?呵呵
我最近 n 个程序为了给客户推卸责任,都是用函数指针处理错误:)
jack_wq 2003-08-19
  • 打赏
  • 举报
回复
一般函数指针不多用!
yellowwolf 2003-08-19
  • 打赏
  • 举报
回复
#include<iostream.h>
int max(int x,int y)
{
if(x>y)return x;
else return y;
}
void main()
{
int max(int,int);
int a,b,c;
int (*p)(int, int);
cin>>a>>b;
p=&max;
c=(*p)(a,b);
cout<<c;
}
napoleonpan1 2003-08-19
  • 打赏
  • 举报
回复
谢过,调试通过
junglesong 2003-08-19
  • 打赏
  • 举报
回复
#include<iostream.h>
int max(int x,int y)
{
if(x>y)return x;
else return y;
}
void main()
{
int max(int,int);
int a,b,c;
int (*p)(int,int);//这里修正一下就可以了。
cin>>a>>b;
p=max;//出错显示cannot conver int(*)(int,int) to int(*)()
c=(*p)(a,b);
cout<<c;
}
sevecol 2003-08-19
  • 打赏
  • 举报
回复
max是返回值是int,有两个参数(都是int)的函数类型。
而p是无参数,返回值是int的类型。
应该这样定义p
int (*p)(int,int)

69,336

社区成员

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

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