函数做参数问题

skyphantom 2005-10-10 08:48:55
//下列段代码编译不通过,请高手指点:
// main_3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

/******************************************/
void hm_retrun(int hm_x1, int &hm_y1)
{
hm_y1 = 2*hm_x1;
}

void hm_main_test( int hm_main_x1, void hm_retrun(int hm_x1, int &hm_y1), int &hm_main_y1)
{
int hm_x1 = 3;
int hm_y1;
hm_retrun( hm_x1, hm_y1);
hm_main_y1 = hm_y1;
}
/*****************************************/

int main(int argc, char* argv[])
{
int hm_main_x1 = 1;
int hm_x1 = 3;
int hm_y1;
int hm_main_y1;
hm_main_test( hm_main_x1, hm_retrun( hm_x1, hm_y1), hm_main_y1);


编译时出现下列信息:
--------------------Configuration: main_3 - Win32 Debug--------------------
Compiling...
main_3.cpp
D:\Tem_test\main_3\main_3.cpp(29) : error C2664: 'hm_main_test' : cannot convert parameter 2 from 'void' to 'void (__cdecl *)(int,int &)'
Expressions of type void cannot be converted to other types
D:\Tem_test\main_3\main_3.cpp(69) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
Creating browse info file...

main_3.exe - 1 error(s), 1 warning(s)
...全文
172 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyphantom 2005-10-10
  • 打赏
  • 举报
回复
感谢各位高手。
snowbirdfly 2005-10-10
  • 打赏
  • 举报
回复
但是还是建议楼主注意代码命名~~~
要不然容易出错~而且修改不容易~~~
snowbirdfly 2005-10-10
  • 打赏
  • 举报
回复
恩,楼上说的有理~为了以后不被炒鱿鱼~~~
呵呵~~
wohow 2005-10-10
  • 打赏
  • 举报
回复
如果在一家公司这样写代码,后来者如何维护?老板不大会炒你了。
megaboy 2005-10-10
  • 打赏
  • 举报
回复
hm_main_test( hm_main_x1, hm_retrun( hm_x1, hm_y1), hm_main_y1);

这样调用hm_main_test是错误的,hm_return(hm_x1, hm_y1)表示调用hm_return然后把它的返回值作为实参传递给hm_main_test,但hm_main_test的这个形参是要求一个函数。因此hm_main_test( hm_main_x1, hm_retrun,hm_main_y1);这样才是正确的。
niefuhua 2005-10-10
  • 打赏
  • 举报
回复
我建议你改一下自己命名风格,你这样的代码很难看!!!
niefuhua 2005-10-10
  • 打赏
  • 举报
回复
你这样就通过了!!!

//下列段代码编译不通过,请高手指点:
// main_3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

/******************************************/
//定义一个函数指针类型
typedef void (*PF)(int,int&);

void hm_retrun(int hm_x1, int &hm_y1)
{
hm_y1 = 2*hm_x1;
}

void hm_main_test( int hm_main_x1, PF pf, int &hm_main_y1)
{
int hm_x1 = 3;
int hm_y1;
hm_retrun( hm_x1, hm_y1);
hm_main_y1 = hm_y1;
}
/*****************************************/

int main(int argc, char* argv[])
{
int hm_main_x1 = 1;
int hm_x1 = 3;
int hm_y1;
int hm_main_y1;
hm_main_test( hm_main_x1, hm_retrun, hm_main_y1);

return 1;
}
qfeng_zhao 2005-10-10
  • 打赏
  • 举报
回复
hm_main_test( hm_main_x1, hm_retrun( hm_x1, hm_y1), hm_main_y1);
改为
hm_main_test( hm_main_x1, hm_retrun,hm_main_y1);

试试

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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