C++ 中的函数指针定义不工作
我无法解决这个错误: 代码:
class myClass
{
public:
void callMain() ;
void (*callme)(int a , int b);
}
void myClass::callMain()
{
callSomeApi(callme, <some arguments>); //callme function pointer is passed as argument
}
void (myClass :: *callme) (int a, int b) // it this the correct way to define a function pointer
{
}
我在 Visual Studio 2008 中收到以下错误 错误C2470:callme看起来像函数定义,但没有参数列表;跳过明显的身体
`编辑: 我的想法: 1.我想在类myClass中创建成员函数指针 2. 在范围之外定义它。 3. 将该函数指针作为参数传递到某个 Api 函数中。
I am not able to solve this error:
CODE:
class myClass
{
public:
void callMain() ;
void (*callme)(int a , int b);
}
void myClass::callMain()
{
callSomeApi(callme, <some arguments>); //callme function pointer is passed as argument
}
void (myClass :: *callme) (int a, int b) // it this the correct way to define a function pointer
{
}
I get the following error in Visual Studio 2008
error C2470: callme looks like a function definition, but there is no parameter list; skipping apparent body`
EDIT:
My idea:
1. i want to create member function pointer in class myClass
2. Define it outside scope.
3. Pass that function pointer as a parameter in some Api function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你正在尝试做这样的事情,但我可能是错的
I think you are trying to do something like this, but I may be wrong
很难说出你想在这里做什么。您是否要将
callme
定义为方法,然后将指针传递给它作为callSomeApi()
的第一个参数?在这种情况下,只需将其定义为普通的旧方法,然后在callMain()
中输入callSomeApi(&callme, ...)
:您不需要函数您班级中的指针成员。如果您出于某种原因需要函数指针作为成员,那么第 5 行中的声明是正确的,您可以输入 callme = &someFunction 将指针设置为指向 someFunction( ),这必须在其他地方定义。
It is difficult to tell what you're trying to do here. Do you want to define
callme
as a method, and then pass a pointer to it as the first parameter ofcallSomeApi()
? In that case, just define it as a plain old method, and typecallSomeApi(&callme, ...)
incallMain()
: you do not need a function pointer member in your class.If you need a function pointer as a member for whatever reason, then your declaration in line 5 is correct, and you can type
callme = &someFunction
to set the pointer to point tosomeFunction()
, which would have to be defined somewhere else.是啊,但是为什么会有尸体呢?
callme
是一个成员(函数指针)。编辑我不知道为什么这被否决了。
我们不会到处写,
因为
int (*a)(int, int);
定义了一个类型为int (*)(int, int)
。如果 opener 想要使用函数作为
callSomeApi
中的回调,他需要做并希望 callSomeApi 接受 pmfs 或函数对象(并使用
bind 或类似技术)
编辑以进行开场白的更改。
指向成员函数的指针是一个指针。它不是一个函数。
为了说明这一点,
定义了一个函数
a
。该函数的类型为int ()
。函数也有函数体。然而,函数指针是通过以下方式定义的:
在本例中,
f
是一个函数指针。它没有有函数体(因为它不是函数)。我们可以通过
=
分配给函数指针:f = &a;
或f = a;
,例如,让我们这样做f();
随后将调用a
。但基本上,函数指针没有主体。指向成员函数的指针也不会。
it is, but why is there a body??
callme
is a member (a function pointer).EDIT i have no idea why this is downvoted.
One does not go around writing
because
int (*a)(int, int);
defines a variablea
which is of typeint (*)(int, int)
.If opener wants to use a function as a callback in
callSomeApi
, he needs to doand hope that callSomeApi accepts pmfs or function objects (and use
bind
or similar techniques)EDIT for opener's changes.
a pointer to member function is a pointer. it is not a function.
To illustrate,
defines a function
a
. This function has typeint ()
. A function also has a function body.However, a function pointer is defined via:
In this case
f
is a function pointer. It does not have a function body (because it is not a function).One can assign to function pointers, via
=
:f = &a;
, orf = a;
, for example, lets us dof();
subsequently which will calla
.But basically, a function pointer doesn't have a body. Neither do pointers to member functions.