Subclassing compile error

F

farseer

Can someone please assist me in understanding why i am receiving this
error: " error C2440: 'type cast' : cannot convert from 'LRESULT
(__cdecl CMyToday::* )(HWND,UINT,WPARAM,LPARAM)' to 'LONG' "

Here is the source snippet that is causing this compile error. The
error is caused by the SetWindowLong line below.

MyDataWin.h

WNDPROC _hOldWndProc;
BOOL Create( HWND hWndParent, DWORD dwStyle = WS_VISIBLE | WS_CHILD
);
LRESULT CALLBACK EditWinProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam );


MyDataWin.cpp

BOOL MyDataWin::Create( HWND hWndParent, DWORD dwStyle )
{

_hEditBox = CreateWindow( L"EDIT", NULL, WS_CHILD | WS_VISIBLE |
ES_AUTOHSCROLL, 2, 2, 100, 20, m_hWnd, NULL, _hInstance, NULL );

_hOldWndProc = SetWindowLong( _hEditBox ,GWL_WNDPROC, (LONG)
EditWinProc );
}

LRESULT CALLBACK MyDataWin::EditWinProc( HWND hWnd, UINT uMsg, WPARAM
wParam, LPARAM lParam )
{
....
....
}
 
A

Alf P. Steinbach

* farseer:
Can someone please assist me in understanding why i am receiving this
error: " error C2440: 'type cast' : cannot convert from 'LRESULT
(__cdecl CMyToday::* )(HWND,UINT,WPARAM,LPARAM)' to 'LONG' "

First of all, see the FAQ item "How do I post a question about code that
doesn't work correctly?", currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

Here is the source snippet that is causing this compile error. The
error is caused by the SetWindowLong line below.

MyDataWin.h

WNDPROC _hOldWndProc;
BOOL Create( HWND hWndParent, DWORD dwStyle = WS_VISIBLE | WS_CHILD
);
LRESULT CALLBACK EditWinProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam );


MyDataWin.cpp

BOOL MyDataWin::Create( HWND hWndParent, DWORD dwStyle )
{

_hEditBox = CreateWindow( L"EDIT", NULL, WS_CHILD | WS_VISIBLE |
ES_AUTOHSCROLL, 2, 2, 100, 20, m_hWnd, NULL, _hInstance, NULL );

_hOldWndProc = SetWindowLong( _hEditBox ,GWL_WNDPROC, (LONG)
EditWinProc );
}

LRESULT CALLBACK MyDataWin::EditWinProc( HWND hWnd, UINT uMsg, WPARAM
wParam, LPARAM lParam )
{
...
...
}

You're using a compiler that allows two things that standard C++ does
not allow:

* Taking the address of a non-static member funtion implicitly.

* Specifying the machine code level calling convention of a member
function.

In short, you shouldn't be using a non-static member function, here your
'EditWinProc', as a C-style callback: a non-static member function must
be called on an object, and a C-style callback isn't.

Assuming a C-style callback is what you're trying to do.

Here are some additional tips:

Don't use prefix underscores in names.

Don't use C-style casts.
 
K

Kai-Uwe Bux

farseer said:
Can someone please assist me in understanding why i am receiving this
error: " error C2440: 'type cast' : cannot convert from 'LRESULT
(__cdecl CMyToday::* )(HWND,UINT,WPARAM,LPARAM)' to 'LONG' "

Here is the source snippet that is causing this compile error. The
error is caused by the SetWindowLong line below.

MyDataWin.h

WNDPROC _hOldWndProc;
BOOL Create( HWND hWndParent, DWORD dwStyle = WS_VISIBLE | WS_CHILD
);
LRESULT CALLBACK EditWinProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam );


MyDataWin.cpp

BOOL MyDataWin::Create( HWND hWndParent, DWORD dwStyle )
{

_hEditBox = CreateWindow( L"EDIT", NULL, WS_CHILD | WS_VISIBLE |
ES_AUTOHSCROLL, 2, 2, 100, 20, m_hWnd, NULL, _hInstance, NULL );

_hOldWndProc = SetWindowLong( _hEditBox ,GWL_WNDPROC, (LONG)
EditWinProc );
}

LRESULT CALLBACK MyDataWin::EditWinProc( HWND hWnd, UINT uMsg, WPARAM
wParam, LPARAM lParam )
{
...
...
}

I think, your compiler is quite explicit about it: in the expression

(LONG) EditWinProc

you are trying to cast a function pointer to LONG -- whatever that maybe.
(Actually, since the code is riddled with non-standard macros, one cannot be
sure about this.) Anyway, such cast is not necessarily possible. The
standard only requires that casts from pointers to integral types are
defined when the integral type is large enough to hold the pointer. Also,
it is not entirely clear from the wording of the standard whether this
provision covers function pointers at all (although that would be a
straight forward interpretation).


Best

Kai-Uwe Bux
 
L

Luke Meyers

farseer said:
Can someone please assist me in understanding why i am receiving this
error:

How to read a compiler error:
" error C2440: 'type cast' :

Okay, it's an error related to typecasting. Also, there is an error
number, which you could look up in your compiler's documentation or
search for on the web.

Now, typecasting is conversion from one type to another. What types
are involved?
cannot convert from 'LRESULT
(__cdecl CMyToday::* )(HWND,UINT,WPARAM,LPARAM)'

Okay, that's what we're trying to convert from. Kind of an ugly type
name, and non-standard, but basically this is a member function of the
class CMyToday, which takes four parameters and returns an LRESULT.
to 'LONG' "

And here's what you're trying to convert it to.
Here is the source snippet that is causing this compile error. The
error is caused by the SetWindowLong line below.

_hOldWndProc = SetWindowLong( _hEditBox ,GWL_WNDPROC, (LONG)
EditWinProc );

Yup, there it is. So, the conversion of EditWinProc to LONG is not
permitted (no such conversion exists, the compiler is telling us). Why
are you attempting it?

In the future, please read the FAQ (http://parashift.com/c++-faq-lite)
before posting here, specifically the sections on how to post code and
what kinds of questions are on-topic. You should post code that
actually compiles, and take questions about platform-specific and/or
non-standard extensions to a more appropriate forum.

Luke
 
F

farseer

Thanks Alf. changing the callback to static resolved the compile issue
(though i am not sure why ye).

btw, where should one post if their code does not compile (since
everyone seems to have an issue with that).
 
J

Jim Langston

farseer said:
Thanks Alf. changing the callback to static resolved the compile issue
(though i am not sure why ye).

btw, where should one post if their code does not compile (since
everyone seems to have an issue with that).

Actually, not everyone had a problem with it. I believe your question was a
legitamite C++ question as did the 3 other people who replied other than
Jonathan.
 
J

Jonathan Mcdougall

Jim said:
Actually, not everyone had a problem with it. I believe your question was a
legitamite C++ question as did the 3 other people who replied other than
Jonathan.

The question was indeed a "legitimate C++ question", but not a
"legitimate comp.lang.c++ question". The FAQ clearly states that
"ltimately this means your question must be answerable by looking
into the C++ language definition as determined by the ISO/ANSI C++
Standard document, and by planned extensions and adjustments."

The OP referred specifically to the Win32 API; the question is about
calling a Win32 function with bad arguments; the code snippet uses a
plethora of non standard things and was not compilable (as per 5.8).

This question was not about a problem with standard C++, it was about a
problem with a proprietary API which the OP was unable to use
correctly. I therefore forwarded him to the FAQ, in which he probably
found the relevant newsgroup.


Jonathan
 
D

Diego Martins

Jonathan said:
This question was not about a problem with standard C++, it was about a
problem with a proprietary API which the OP was unable to use
correctly. I therefore forwarded him to the FAQ, in which he probably
found the relevant newsgroup.

I disagree. His question was clearly about type conversion. The Win32
API was only an unhappy case study
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top