C++ 中的函数指针定义不工作

发布于 2024-10-04 13:47:17 字数 560 浏览 0 评论 0原文

我无法解决这个错误: 代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别再吹冷风 2024-10-11 13:47:17

我认为你正在尝试做这样的事情,但我可能是错的

class MyClass
{
   public:
   typedef void (*funcPtr)(int, int);
   MyClass(funcPtr whatToCall)
   { 
     callme = whatToCall;
   }
   void myClass::callMain()
   {  
       callSomeApi(callme, <some arguments>);  //callme function pointer is passed as argument
   }
private:
   funcPtr callme;
};

void f(int a, int b) {blah blah blah};


int main()
{
   MyClass myObject(f);
   myObject.callMain(); //will call the api function with f passed as argument
}

I think you are trying to do something like this, but I may be wrong

class MyClass
{
   public:
   typedef void (*funcPtr)(int, int);
   MyClass(funcPtr whatToCall)
   { 
     callme = whatToCall;
   }
   void myClass::callMain()
   {  
       callSomeApi(callme, <some arguments>);  //callme function pointer is passed as argument
   }
private:
   funcPtr callme;
};

void f(int a, int b) {blah blah blah};


int main()
{
   MyClass myObject(f);
   myObject.callMain(); //will call the api function with f passed as argument
}
帅气尐潴 2024-10-11 13:47:17

很难说出你想在这里做什么。您是否要将 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 of callSomeApi()? In that case, just define it as a plain old method, and type callSomeApi(&callme, ...) in callMain(): 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 to someFunction(), which would have to be defined somewhere else.

山田美奈子 2024-10-11 13:47:17

是啊,但是为什么会有尸体呢? callme 是一个成员(函数指针)。

编辑我不知道为什么这被否决了。

我们不会到处写,

int (*a)(int, int) {
}

因为 int (*a)(int, int); 定义了一个类型为 int (*)(int, int)

如果 opener 想要使用函数作为 callSomeApi 中的回调,他需要做

class myClass {
    ....
    void callme(int a, int b);
    ....
}

void myClass::callMain() { callSomeApi(&myClass::callme, this, <etc>); }

void myClass::callme(int a, int b) { ... }

并希望 callSomeApi 接受 pmfs 或函数对象(并使用 bind 或类似技术)

编辑以进行开场白的更改。

指向成员函数的指针是一个指针。它不是一个函数。

为了说明这一点,

int a() { return 1; }

定义了一个函数a。该函数的类型为int ()。函数也有函数​​体。

然而,函数指针是通过以下方式定义的:

int (*f)();

在本例中,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

int (*a)(int, int) {
}

because int (*a)(int, int); defines a variable a which is of type int (*)(int, int).

If opener wants to use a function as a callback in callSomeApi, he needs to do

class myClass {
    ....
    void callme(int a, int b);
    ....
}

void myClass::callMain() { callSomeApi(&myClass::callme, this, <etc>); }

void myClass::callme(int a, int b) { ... }

and 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,

int a() { return 1; }

defines a function a. This function has type int (). A function also has a function body.

However, a function pointer is defined via:

int (*f)();

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;, or f = a;, for example, lets us do f(); subsequently which will call a.

But basically, a function pointer doesn't have a body. Neither do pointers to member functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文