Link to home
Start Free TrialLog in
Avatar of ToshiShinohara
ToshiShinohara

asked on

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'

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.
Avatar of jkr
jkr
Flag of Germany image

Does it work when you change your function from 'BOOL' to 'void' as the return type?
Avatar of ToshiShinohara
ToshiShinohara

ASKER

Yes it does and I have other methods with void that don't give error.
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.
Is there any way to get around and define a some return type ?
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.
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);
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
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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