0

I am trying to create a function in VC++ that takes a function pointer but I keep getting syntax errors.

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.

1
  • 1
    You should probably consider using boost::function or std::function, if either is available to you. They're much easier to use and read. Commented Aug 3, 2010 at 15:11

3 Answers 3

9

It's usually a good idea to define a typedef for your function pointer type. 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.

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

And definition:

void CSpreadWnd::ApplyFuncToCellsInSelection(MyFuncPtr func) { ... }
Sign up to request clarification or add additional context in comments.

Comments

6

You have the asterisk on the wrong side of CSpreadWnd:

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

Comments

4

The CSpreadWnd pointer looks funny. It looks like it should be CSpreadWnd* rather than *CSpreadWnd.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.