简体   繁体   English

C ++函数指针语法

[英]C++ Function Pointer Syntax

I am trying to create a function in VC++ that takes a function pointer but I keep getting syntax errors. 我试图在VC ++中创建一个带有函数指针的函数,但是我一直在获取语法错误。

The declaration in my header file looks like this: 我的头文件中的声明如下所示:

 void ApplyFuncToCellsInSelection(void(*func)(CPoint, *CSpreadWnd));

Here is the definition: 这是定义:

void CSpreadWnd::ApplyFuncToCellsInSelection(void(*func)(CPoint, *CSpreadWnd)) { ... }

And here are the error messages I'm getting: 这是我收到的错误消息:

c:\...\spreadwnd.h(274) : error C2059: syntax error : 'function-style cast'
c:\...\spreadwnd.h(274) : error C2059: syntax error : ')'
c:\...\spreadwnd.h(274) : error C2143: syntax error : missing ')' before ';'

I know its probably something really simple that I'm missing but I can't seem to figure it out. 我知道我可能很简单,但是我似乎无法弄清楚。

It's usually a good idea to define a typedef for your function pointer type. 为函数指针类型定义typedef通常是一个好主意。 It helps using it in further declarations, and avoids having to change it twice when you write an error. 它有助于在进一步的声明中使用它,并避免在编写错误时必须对其进行两次更改。 Here, you put the asterisk on the wrong side of CSpreadWnd . 在这里,您将星号放在CSpreadWnd的错误一侧。

typedef void (*MyFuncPtr)(CPoint, CSpreadWnd*);
void ApplyFuncToCellsInSelection(MyFuncPtr func);

And definition: 和定义:

void CSpreadWnd::ApplyFuncToCellsInSelection(MyFuncPtr func) { ... }

You have the asterisk on the wrong side of CSpreadWnd : 您在CSpreadWnd错误的一面有星号:

void ApplyFuncToCellsInSelection(void(*func)(CPoint, CSpreadWnd*));
                                 the asterisk needs to go here ^

The CSpreadWnd pointer looks funny. CSpreadWnd指针看起来很有趣。 It looks like it should be CSpreadWnd* rather than *CSpreadWnd . 看起来应该是CSpreadWnd*而不是*CSpreadWnd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM