Solved

'static_cast' : cannot convert from 'void error

Posted on 2009-05-11
4
1,287 Views
Last Modified: 2013-12-21
I am converting an eVC++ project to VS2005.
The ON_MESSAGE(...) line below
BEGIN_MESSAGE_MAP(CBegin, CDialog)
//{{AFX_MSG_MAP(CBegin)
ON_MESSAGE(WM_USER_REFRESH_FULLSCREEN, OnFullScreenRefresh)

gives the error:
error C2440: 'static_cast' : cannot convert from 'void (__cdecl CBegin::* )(void)' to 'LRESULT (__cdecl CWnd::* )(WPARAM,LPARAM)'      

How do I solve this?
0
Comment
Question by:Wanting2LearnMan
  • 2
4 Comments
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 200 total points
ID: 24354351
Change OnFullScreenRefresh() to

LRESULT CBegin::OnFullScreenRefresh( WPARAM wParam, LPARAM lParam )
{
   ...

   return 0L;
}


May be macro ON_MESSAGE is changed.
0
 
LVL 24

Assisted Solution

by:alexey_gusev
alexey_gusev earned 100 total points
ID: 24354491
ON_MESSAGE ()  macro always was with LRESULT foo(WPARAM, LPARAM), so older versions of MFC allowed some freedom, but no more
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 200 total points
ID: 24355353
MFC older version have different behavior because ON_MESSAGE macros are different (look at the cast)...
 // old

 #define ON_MESSAGE(message, mbrFn) \

     {message, 0, 0, 0, AfxSig_lwl, \

     (AFX_PMSG)(AFX_PMSGW)(LRESULT \

     (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))&mbrFn },
 

 // new 

 #define ON_MESSAGE(message, memberFxn) \

    { message, 0, 0, 0, AfxSig_lwl, \

        (AFX_PMSG)(AFX_PMSGW) \

        (static_cast< LRESULT (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM) > \

        (memberFxn)) },

Open in new window

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 200 total points
ID: 24357148
>>>> have different behavior because ON_MESSAGE macros are different (look at the cast)...

To add to above information:

A C cast was able to *cast off* the arguments of a function pointer while C++ static_cast requires same argument list of the function being casted and the cast only turns the class from YourDialogClass to CWnd (what is a baseclass).

The issue is that the call from DispatchMessage actually passed WPARAM and LPARAM arguments at the stack but your function doesn't have arguments. One could think of problems when unrolling the stack but it should have no consequences cause the stack was freed by the caller  (and not by the callee).

Things were different if your function would have 3 arguments. Then the last argument actually was not supported by the caller and if your function would access it nevertheless it would access some undefined (arbitrary) memory address what even could lead to a crash.

So, the above is a sample where a static_cast has advantages before a C cast with the constraint that a function with lesser arguments would need a different cast or must get some dummy arguments not actually needed just to please the compiler ...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now