2
votes

I have a Visual C++ 6.0 project which I need to update and I import it to Visual Studio 2012 with no problems, however, during compilation, I get the following four errors:

Error 1 error C2440: 'static_cast' : cannot convert from 'void (__thiscall CTrendDlg::* )(int)' to 'void (__thiscall CCmdTarget::* )(UINT)' c:\users\nima\desktop\ffls_scode\trenddlg.cpp 89

Error 6 error C2440: 'static_cast' : cannot convert from 'void (__thiscall CManualDlg::* )(int)' to 'void (__thiscall CCmdTarget::* )(UINT)' c:\users\nima\desktop\ffls_scode\manualdlg.cpp 175

Error 7 error C2440: 'static_cast' : cannot convert from 'void (__thiscall CManualDlg::* )(int)' to 'void (__thiscall CCmdTarget::* )(UINT)' c:\users\nima\desktop\ffls_scode\manualdlg.cpp 177

Error 8 error C2440: 'static_cast' : cannot convert from 'void (__thiscall CManualDlg::* )(int)' to 'void (__thiscall CCmdTarget::* )(UINT)' c:\users\nima\desktop\ffls_scode\manualdlg.cpp 178

The first error is originating from the following line of code, and the rest are similar:

ON_COMMAND_RANGE(IDC_CHECK_PEN, IDC_CHECK_GRID, OnCheckButtons)
ON_COMMAND_RANGE(IDC_REF_L1, IDC_REF_L16, OnCarriagePos)
ON_COMMAND_RANGE(IDC_VALVE_L1, IDC_VALVE_L4, OnValve)
ON_COMMAND_RANGE(IDC_SAMPLE_L_A, IDC_SAMPLE_L_B, OnDetector)

Where (as an instance) OnCheckButtons function is defined in TrendDlg.cpp as follows:

void CTrendDlg::OnCheckButtons(int id) 
{
    UINT    state;
    RECT    rect = {m_rect.left-60, m_rect.top-10, m_rect.right+40,       m_rect.bottom+30};

state = ((CButton*)GetDlgItem(id))->GetState();
if ((state & 0x0003) == 1)
{
    switch (id)
    {
    case    IDC_CHECK_PEN:
        m_pen = TRUE;
        break;
    case    IDC_CHECK_LINE:
        m_line = TRUE;
        break;
    case    IDC_CHECK_BUBBLES:
        m_bubble = TRUE;
        break;
    case    IDC_CHECK_GRID:
        m_grid = TRUE;
    }
}
else
{
    switch (id)
    {
    case    IDC_CHECK_PEN:
        m_pen = FALSE;
        break;
    case    IDC_CHECK_LINE:
        m_line = FALSE;
        break;
    case    IDC_CHECK_BUBBLES:
        m_bubble = FALSE;
        break;
    case    IDC_CHECK_GRID:
        m_grid = FALSE;
    }
}
InvalidateRect(&rect);  

}

my message map range is also defined as:

  BEGIN_MESSAGE_MAP(CTrendDlg, CDialog)

Why is the compiler trying to cast CTrendDlg and CManualDlg type to CCmdTarget type? is there a change in class structure from VC 6.00 to VS2012?

I appreciate your help.

2
According to the documentation, the parameter of the handler function should be an unsigned int -- you have it as a signed int.Dan Mašek
Thanks, such a simple issue, I thought the compiler would neglect the slight difference like that in function prototype!Nima Nikvand
They are two distinct types each with a different range of values, that's a more than a slight difference, and certainly nothing a compiler should neglect. As marcinj mentions, the C++ compiler in VS6 is certainly not the epitome of standards compliance, so don't expect code to be correct just because it didn't complain.Dan Mašek

2 Answers

5
votes

Your handler should be defined like this:

void CTrendDlg::OnCheckButtons(UINT id) 

So basically change int to UINT

2
votes

See here how this macro ON_COMMAND_RANGE should be used:

1) You need a proper signature, return void and the parameter should be of type UINT

2) Qualify your method with class name:

ON_COMMAND_RANGE(IDC_CHECK_PEN, IDC_CHECK_GRID, &CTrendDlg::OnCheckButtons)
                                                ^^^^^^^^^^^^

VS 6.0 is quite old, modern VS versions are more standard conformant.