template question.

T

tather

I found these code in http://www.parkscomputing.com
but I can not compile it successful.

------------------------------------------------

typedef LONG (__stdcall * forwardFn_t)(HWND, UINT, WPARAM, LPARAM);
template<UINT msg> class Handler;
template<> class Handler<WM_PAINT>
{
protected:
virtual void OnPaint() = 0;
public:
LRESULT Handle(WPARAM wParam, LPARAM lParam)
{
OnPaint();
return 0L;
}
static void Forward(HWND hWnd, forwardFn_t pfn)
{
(*pfn)(hWnd, msg, 0L, 0L);
}
};
 
J

Jim Langston

tather said:
I found these code in http://www.parkscomputing.com
but I can not compile it successful.

------------------------------------------------

typedef LONG (__stdcall * forwardFn_t)(HWND, UINT, WPARAM, LPARAM);
template<UINT msg> class Handler;
template<> class Handler<WM_PAINT>
{
protected:
virtual void OnPaint() = 0;
public:
LRESULT Handle(WPARAM wParam, LPARAM lParam)
{
OnPaint();
return 0L;
}
static void Forward(HWND hWnd, forwardFn_t pfn)
{
(*pfn)(hWnd, msg, 0L, 0L);
}
};

It most likely means it doesn't know what UINT means, so msg is undeclared.
It's common for some compilers to give an error on the next line, or next
syntax word it finds.

I'm sure if you do:
typedef unsigned int UINT;
before that line it will get past that error, but then it'll probably bitch
about <WM_PAINT> etc... This is windows code and expects windows headers.
Try including <windows.h>
 
B

Bo Persson

tather wrote:
:: I found these code in http://www.parkscomputing.com
:: but I can not compile it successful.
::
:: ------------------------------------------------
::
:: typedef LONG (__stdcall * forwardFn_t)(HWND, UINT, WPARAM, LPARAM);
:: template<UINT msg> class Handler;
:: template<> class Handler<WM_PAINT>
:: {
:: protected:
:: virtual void OnPaint() = 0;
:: public:
:: LRESULT Handle(WPARAM wParam, LPARAM lParam)
:: {
:: OnPaint();
:: return 0L;
:: }
:: static void Forward(HWND hWnd, forwardFn_t pfn)
:: {
:: (*pfn)(hWnd, msg, 0L, 0L);
:: }
:: };
:: ----------------------------------------------------
:: error C2065: 'msg' : undeclared identifier
::
:: Why the error occur, and How can I resolve it to get the same
:: fuction?

The name msg is the template parameter of the Handler class. In the
specialization of this class, you just have template<>, and the msg
name isn't visible. In this case, its value is WM_PAINT, as this is
what it is specialized for.

That it once used to compile is really a bug that has been fixed in
later editions if the compiler!



Bo Persson
 
J

James Kanze

I found these code inhttp://www.parkscomputing.com
but I can not compile it successful.

typedef LONG (__stdcall * forwardFn_t)(HWND, UINT, WPARAM, LPARAM);
template<UINT msg> class Handler;
template<> class Handler<WM_PAINT>
{
protected:
virtual void OnPaint() = 0;
public:
LRESULT Handle(WPARAM wParam, LPARAM lParam)
{
OnPaint();
return 0L;
}
static void Forward(HWND hWnd, forwardFn_t pfn)
{
(*pfn)(hWnd, msg, 0L, 0L);
}
};
Why the error occur, and How can I resolve it to get the same fuction?

The error occured (obviously) because there is no symbol msg
declared which is visible in the scope of the function. In
fact, the only symbol msg I see anywhere is as an argument to
the template declaration; since it is a declaration, and not a
definition, the symbol is really only a comment, and not
accessible anywhere.

What did you mean instead of msg? WM_PAINT? Or something else?
 

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,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top