Solved

'static_cast' : cannot convert from 'void error

Posted on 2009-05-11
4
1,305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

696 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