函数指针参数

genship 2011-11-23 07:23:45
声明了一个带参数的函数指针void(*ptr)(int s))

现在调用的时候出现[C++ Error] Unit1.cpp(21): E2451 Undefined symbol 's'
请问如何调用把参数传入函数func(int s)中?
[code=C/C++]

void caller(void(*ptr)(int s))

{
ptr(s); /* 调用ptr指向的函数 */
}

void func(int s)
{
ShowMessage(IntToStr(s));
}

int test()

{
caller(func); /* 传递函数地址到调用者 */
}
[code]
...全文
251 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbzjzsb123 2011-11-24
  • 打赏
  • 举报
回复
anyin89 2011-11-24
  • 打赏
  • 举报
回复
确实搞得太复杂
[Quote=引用 15 楼 jha334201553 的回复:]

引用 13 楼 genship 的回复:
C/C++ code


void caller(void(*ptr)(int),int s)
{
ptr(s); /* 调用ptr指向的函数 */
}



这样是可以的。只有外加一个参数给他了吗?
如果func有3、5个参数

C/C++ code


void func(int p1, int p2, int p3……
http://www.purji.com/list-13.htm
[/Quote]
小方fws 2011-11-24
  • 打赏
  • 举报
回复
编译之后void caller(void(*ptr)(int s))
这里面的s是没用的(只是形式而已),可以用再加一个参数的方式解决
do__i 2011-11-24
  • 打赏
  • 举报
回复
假设函数 void func(int s)
void (*ptr)(int s); ptr的原型是:void (*)(int s)
ptr或*ptr 即是调用了func.
karorokiki 2011-11-23
  • 打赏
  • 举报
回复
使用va_list : C语言中的可变参数,如何呢

[Quote=引用 13 楼 genship 的回复:]
C/C++ code


void caller(void(*ptr)(int),int s)
{
ptr(s); /* 调用ptr指向的函数 */
}



这样是可以的。只有外加一个参数给他了吗?
如果func有3、5个参数

C/C++ code


void func(int p1, int p2, int p3 ,char c)
{
i……
[/Quote]
「已注销」 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 genship 的回复:]
C/C++ code


void caller(void(*ptr)(int),int s)
{
ptr(s); /* 调用ptr指向的函数 */
}



这样是可以的。只有外加一个参数给他了吗?
如果func有3、5个参数

C/C++ code


void func(int p1, int p2, int p3 ,char c)
{
i……
[/Quote]

你的什么函数啊,怎么多参数,你调用函数的开销多大啊,晕倒,要么直接用数组,要么蛋疼的不管效率用不定参数的方式声明
void callervoid(*ptr)(int TotalValue,...)
{
va_list args;
va_start(args, TotalValue);
//处理这些数据……
va_end(args);
}
「已注销」 2011-11-23
  • 打赏
  • 举报
回复
去看下typedef的用法就好了,这个很容易解决的,要么全局变量,要么参数~~,你第一个贴参数没传,不管怎么定义都是错的~~
genship 2011-11-23
  • 打赏
  • 举报
回复

void caller(void(*ptr)(int),int s)
{
ptr(s); /* 调用ptr指向的函数 */
}


这样是可以的。只有外加一个参数给他了吗?
如果func有3、5个参数

void func(int p1, int p2, int p3 ,char c)
{
int t = p1+p2+p3+c ;
}


那caller是要这样:

void caller(void(*ptr)(int,int,int,char),int p1,int p2,int p3,char c)

吗?
「已注销」 2011-11-23
  • 打赏
  • 举报
回复
typedef void( *ptr)( int s );

void caller(ptr fun,int s)
{
fun(s); /* 调用ptr指向的函数, s是函数参数 */
}

void func(int s)
{
ShowMessage(IntToStr(s));
}

int test()
{
caller(func, 1); /* 传递函数地址到调用者 */
return 0;
}

结贴吧!!
iamnobody 2011-11-23
  • 打赏
  • 举报
回复
int WINAPI sio_cnt_irq(int port, VOID (CALLBACK *func)(int ), int count);
这样声明。
genship 2011-11-23
  • 打赏
  • 举报
回复
mingliang1212,icechenbing 两位的说法是对的,外加一个参数。可是我的问题在于
int WINAPI sio_cnt_irq(int port, VOID (CALLBACK *func)(int port), int count);函数不允许我再加一个参数
而且sio_cnt_irq()函数的实现目前不受我控....

sio_cnt_irq(Aport, ComIrq, count)
如何让ComIrq接受我从
bool CommInit(int Aport) 函数传入的Aport ?

void CALLBACK ComIrq(int Aport)
{
GetDataFromSerialPort(Aport);
}

刚看了pathuang68(玄机逸士) 的相关文章,文章写得很好,但是貌似没有提及函数指针传参问题(也许是在下才疏学浅,没悟出,请指点)。感谢各位的提点。
quwei197874 2011-11-23
  • 打赏
  • 举报
回复
楼主在声明中把那个s去掉就对了
genship 2011-11-23
  • 打赏
  • 举报
回复
bool CommInit(int Aport)
{
if(sio_cnt_irq(Aport,ComIrq,1)!=SIO_OK)
{
ShowMessage("设置串口特性失败");
sio_close(Aport);
return false;
}
}

ComIrq 是个Callback 函数。

void CALLBACK ComIrq(int Aport)
{
GetDataFromSerialPort(Aport);
}

sio_cnt_irq 是一个动态库的函数。
int WINAPI sio_cnt_irq(int port, VOID (CALLBACK *func)(int port), int count);

如何把Aport的参数传入ComIrq中?
pathuang68 2011-11-23
  • 打赏
  • 举报
回复
iamnobody 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 genship 的回复:]
to icechenbing,qq120848369:

void caller(void(*ptr)(int)) 是什么意思呢?
也许我刚没表述清楚。

从test()函数中传入一个int 的参数到 ptr(s) 函数中。
test()
{
int param;
param = 1;
caller(func);
}
这里的param =1 的值如何传入到fu……
[/Quote]

必须要两个参数,一个是函数的指针,另个是他的参数int s;
如下:
void caller(void(*ptr)(int ),int s)
{
ptr(s); /* 调用ptr指向的函数 */---[C++ Error]Undefined symbol 's'。
}
Snight 2011-11-23
  • 打赏
  • 举报
回复
typedef void(* Fptr)(int s);

void caller(Fptr ptr,int s)
{
ptr(s); /* 调用ptr指向的函数 */
}

void func(int s)
{
ShowMessage(IntToStr(s));
}

int test()
{
caller(func, 1); /* 传递函数地址到调用者 */
return 1;
}
maoxing63570 2011-11-23
  • 打赏
  • 举报
回复
void caller(void(*ptr)(int)) 是什么意思呢?
也许我刚没表述清楚。
======================================
没,哥哥,你表达的很清楚,你标题不说了函数指针么
genship 2011-11-23
  • 打赏
  • 举报
回复
to icechenbing,qq120848369:

void caller(void(*ptr)(int)) 是什么意思呢?
也许我刚没表述清楚。

从test()函数中传入一个int 的参数到 ptr(s) 函数中。
test()
{
int param;
param = 1;
caller(func);
}
这里的param =1 的值如何传入到func(int s)中,然后show.
void func(int s)
{
ShowMessage(IntToStr(s));
}

其中的参数如何写。
按照我最上面写的编译时候提示

现在调用的时候出现[C++ Error] Unit1.cpp(21): E2451 Undefined symbol 's'
请问如何调用把参数传入函数func(int s)中?
[code=C/C++][code]
void caller(void(*ptr)(int s))
{
ptr(s); /* 调用ptr指向的函数 */---[C++ Error]Undefined symbol 's'。
}

qq120848369 2011-11-23
  • 打赏
  • 举报
回复
void caller(void(*ptr)(int))
恨天低 2011-11-23
  • 打赏
  • 举报
回复
void caller(void(*ptr)(int),int s)

{
ptr(s); /* 调用ptr指向的函数 */
}

64,648

社区成员

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

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