Solved

'static_cast' : cannot convert from 'void error

Posted on 2009-05-11
4
1,293 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best method to protect Android Lollypop phone 3 93
block certain incoming call for cetain time 5 69
VMWare Workspace ONE 10 100
Unable to open debugger port in Intellij idea 6 225
When I was a beginner in Windows Mobile programming, even for small test I used to create a single MFC dialog-based application with one button, and put my test code in the OnLButtonDown method. For Pocket PC it worked fine, on some Windows CE d…
If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
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.

777 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