Casting error in C++

2019-08-31 04:24发布

问题:

Can someone help me with this error ? I'm C++ newbie. And it seems the error occurs right in a bunch of macros. What can I do to solve it ? Or how can I track it down to the source ?

I don't really understand the error. Does it mean the compiler tried to convert the method void ReadCPUparameter() to a LRESULT funcName(WPARAM wParam, LPARAM lParam) function header ?

Error:

// error C2440: 'static_cast' : cannot convert from
//     'void (__thiscall CStartup::* )(void)' to
//     'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'.
//
// ON_MESSAGE(WM_UPLOAD_CPU_PARAMETER,ReadCPUparameter) // error here

(I didn't write this. I need to recompile an old project from Win2000 on a Win7 machine. Old VS6.0 project -> VS2010 Prof.)

Code:

// class CStartup : public CDialog {};

#include "stdafx.h"
#include "MU.h"
#include "Startup.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CStartup::CStartup(CWnd* pParent /*=NULL*/) : CDialog(CStartup::IDD, pParent)
{
    p_p = &cpu_par;
}

void CStartup::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CStartup, CDialog)
    ON_WM_SHOWWINDOW()
    ON_MESSAGE(WM_UPLOAD_CPU_PARAMETER,ReadCPUparameter) // error here
END_MESSAGE_MAP()

const int nLanguageIds_Language[] =
{
    // ...
};


#define MAX_READINGS    200

BOOL CStartup::OnInitDialog() 
{
    // ...
}

void CStartup::OnOK() 
{   
    CDialog::OnOK();
}

int CStartup::Check_OnRead() 
{
    // ...
}

void CStartup::ReadCPUparameter() 
{
    // ...
}

void CStartup::OnShowWindow(BOOL bShow, UINT nStatus) 
{
    CDialog::OnShowWindow(bShow, nStatus);
    PostMessage( WM_UPLOAD_CPU_PARAMETER );     
}

回答1:

the code behind the ON_MESSAGE macro expected ReadCPUparameterto have the following signature: 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'. since the actual signature differs it complain about type incompatibility of the two function pointers. Its like passing a struct Oranges* to a function that expects a struct Apples*.

I guess CDialog inherits from CWND, so all you have to do to change your function signature to

LRESULT Startup::ReadCPUparameter(WPARAM wparam, LPARAM lparam);


标签: c++ casting mfc