Solved

'static_cast' : cannot convert from 'void error

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Account Lockout 12 32
Galaxy note 5 issue with new Android 6.0.1 update 7 385
Airwatch MDM and corporate applications 6 99
I could not build boost code, 10 78
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

932 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

11 Experts available now in Live!

Get 1:1 Help Now