带void参数的typedef函数 [英] typedef function with void parameters
问题描述
这不会编译:
,----
| int foo(char * p)
| {}
|
| int main(int argc,char * argv [])
| {
| typedef int(* FUNC)(void *);
|
| FUNC f = foo;
| }
` ----
错误是:
,----
| gcc -O2 /Users/william/studio/helloworlds/foo.cpp -lm -o foo
| /Users/william/studio/helloworlds/foo.cpp:在函数''int main(int,char **)''中:
| /Users/william/studio/helloworlds/foo.cpp:26:错误:从''int(*)(char *)''无效转换为''int(*)(void *)''
` ----
-
William
http://williamxu.net9.org
This won''t compile:
,----
| int foo(char *p)
| {}
|
| int main(int argc, char *argv[])
| {
| typedef int (*FUNC)(void *);
|
| FUNC f = foo;
| }
`----
The error is:
,----
| gcc -O2 /Users/william/studio/helloworlds/foo.cpp -lm -o foo
| /Users/william/studio/helloworlds/foo.cpp: In function ''int main(int, char**)'':
| /Users/william/studio/helloworlds/foo.cpp:26: error: invalid conversion from ''int (*)(char*)'' to ''int (*)(void*)''
`----
--
William
http://williamxu.net9.org
推荐答案
William徐写道:
William Xu wrote:
这不会编译:
,----
| int foo(char * p)
| {}
|
| int main(int argc,char * argv [])
| {
| typedef int(* FUNC)(void *);
|
| FUNC f = foo;
| }
` ----
错误是:
,----
| gcc -O2 /Users/william/studio/helloworlds/foo.cpp -lm -o foo
| /Users/william/studio/helloworlds/foo.cpp:在函数''int main(int,char **)''中:
| /Users/william/studio/helloworlds/foo.cpp:26:错误:从''int(*)(char *)''无效转换为''int(*)(void *)''
` ----
This won''t compile:
,----
| int foo(char *p)
| {}
|
| int main(int argc, char *argv[])
| {
| typedef int (*FUNC)(void *);
|
| FUNC f = foo;
| }
`----
The error is:
,----
| gcc -O2 /Users/william/studio/helloworlds/foo.cpp -lm -o foo
| /Users/william/studio/helloworlds/foo.cpp: In function ''int main(int, char**)'':
| /Users/william/studio/helloworlds/foo.cpp:26: error: invalid conversion from ''int (*)(char*)'' to ''int (*)(void*)''
`----
所以,你想要哪个 - 一个函数采用`void *`或函数采取
a`char *`?他们不兼容类型。选择一个。
-
克里斯不能将一夸脱放入磁力罐中 Dollin
Hewlett-Packard Limited注册办公室:Cain Road,Bracknell,
注册号:690597 England Berks RG12 1HN
So, which do you want -- a function taking a `void*` or a function taking
a `char*`? They''re not compatible types. Pick one.
--
Chris "can''t fit a quart into a magnetic pot" Dollin
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
Chris Dollin< ch ********** @hp.comwrites:
Chris Dollin <ch**********@hp.comwrites:
所以,哪个做你想要的 - 一个函数采用`void *`或函数服用
a`char *`?他们不兼容类型。选一个。
So, which do you want -- a function taking a `void*` or a function taking
a `char*`? They''re not compatible types. Pick one.
哦,我认为大多数指针类型都可以转换/存储到
void指针中。我正在尝试将不同的函数指针包装成FUNC,
喜欢,
,----
| int foo(char * p){}
| int bar(int * p){}
|
| typedef int(* FUNC)(void *);
|
| FUNC f1 = foo,f2 = bar;
` ----
所以,这是不允许的?
-
William
http: //williamxu.net9.org
William Xu写道:
William Xu wrote:
Chris Dollin< ch**********@hp.comwrites:
Chris Dollin <ch**********@hp.comwrites:
>那么,你想要哪个 - 一个函数采用`void *`或者是一个char *的函数?他们不兼容类型。选一个。
>So, which do you want -- a function taking a `void*` or a function taking
a `char*`? They''re not compatible types. Pick one.
哦,我认为大多数指针类型都可以转换/存储到
void指针中。我正在尝试将不同的函数指针包装成FUNC,
喜欢,
,----
| int foo(char * p){}
| int bar(int * p){}
|
| typedef int(* FUNC)(void *);
|
| FUNC f1 = foo,f2 = bar;
` ----
那么,这是不允许的?
Oh, i thought that most pointer types could be converted/stored into
void pointer. I''m trying to wrap different function pointers into FUNC,
like,
,----
| int foo(char *p){}
| int bar(int *p){}
|
| typedef int (*FUNC)(void *);
|
| FUNC f1 = foo, f2 = bar;
`----
So, this is not allowed?
否 - 因为函数类型与
函数变量的类型不同。
你也许可以强迫它: -
FUNC f1 =(FUNC)foo,f2 =(FUNC)bar;
但是让我们从另一个角度看它....
如果你这样做然后叫
f2(我的狗有跳蚤) ;
您希望酒吧收到什么?
No - because the function types are different to the type of the
function variable.
You could perhaps force it :-
FUNC f1 = (FUNC)foo, f2 = (FUNC)bar;
But let''s look at it from another angle....
If you did this and then called
f2("my dog has fleas");
what do you expect bar to receive?
这篇关于带void参数的typedef函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!