Solved

compiler error while converting vs c++6 project to vs c++ 2005 ... error error C2440: 'static_cast' : cannot convert from 'BOOL’ to to 'AFX_PMSG'

Posted on 2006-06-09
9
1,121 Views
Last Modified: 2013-11-20
This question is related with MFC code that compiles on VS C++6 and now converting to VS C++ 2005.

In my Dialog box

class CEngOLCDlgBLAmbTemp : public CDialog

I have  button and when user clicks button, its message IDOK for ON_BN_CLICKED is captured in the code using method OnBLOK. I do some error checking and then decide wheather to close the dialog box or not.

The methods OnBLOK returns BOOL.

afx_msg BOOL OnBLOK();

BEGIN_MESSAGE_MAP(CEngOLCDlgBLAmbTemp, CDialog)
      //{{AFX_MSG_MAP(CEngOLCDlgBLAmbTemp)
      ON_BN_CLICKED(IDOK, OnBLOK)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()


The compiler error is as follows:

1>c:\envdev-studio2005\envdevelopment-v1.87-vs2005\projects\msat-eol\main\dialogs\engolcdlgblambtemp.cpp(90) : error C2440: 'static_cast' : cannot convert from 'BOOL (__thiscall CEngOLCDlgBLAmbTemp::* )(void)' to 'AFX_PMSG'
1>      None of the functions with this name in scope match the target type

I am using Studio 2005 first time and just trying to get the code to compile.

Any help is appreciated.
0
Comment
Question by:ToshiShinohara
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16873538
Does it work when you change your function from 'BOOL' to 'void' as the return type?
0
 

Author Comment

by:ToshiShinohara
ID: 16873678
Yes it does and I have other methods with void that don't give error.
0
 
LVL 86

Expert Comment

by:jkr
ID: 16873766
Well, the reason is that AFX_PMSG is typedef'd as

typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);

i.e. as 'void' in afxwin.h. The new compiler is obviously stricter than VC6 and does not allow a different return type even though the signatures match.
0
 

Author Comment

by:ToshiShinohara
ID: 16873867
Is there any way to get around and define a some return type ?
0
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.

 
LVL 86

Expert Comment

by:jkr
ID: 16874095
All you need to do is changing the return type of the function(s) in question from 'BOOL' to 'void', then you should get rid of the error.
0
 

Author Comment

by:ToshiShinohara
ID: 16874256
I understand, but it breaks the funtionality of program.

I am not sure how come std. MFC method OnCtlColordoens't return void but compiles.

afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 16875715
What you need is something like
void CMyDlg::OnBtnClick()
{
  TestSomething();
}

and

BOOL CMyDlg()
{
return TRUE;
}

separate the button action from the functionality that tests.


ps. OnCtlColor - that is defined to return a HBRUSH, that is why it compiles correctly in VS 2005
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 16875772
You cannot change message handler prototypes because they are called by MFC Framework. Button message handler has void return type. Changing return type can corrupt the stack. Your old program works, but has hidden bug.
0
 

Author Comment

by:ToshiShinohara
ID: 16877678
Thank you Andy and JKR. I really appreciate your advice from both of you.

By the way, do you know any MFC/C++ Free Lance Programmer in New York/New Jersey ( USA ) area, please conatct me at spjoshi@earthlink.net.

Regards

Santosh
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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

937 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

1 Experts available now in Live!

Get 1:1 Help Now