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,115 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

12 Experts available now in Live!

Get 1:1 Help Now