VC++ hot keys

E

Ed

I've been away from C++ programming for awhile. Maybe this is a simple
problem. I get the error message "illegal call of non-static member
function" when I insert
DWORD hotkey;
hotkey=CHotKeyCtrl::GetHotKey();
into a Wizard-generated function that is NOT static --
void CCubesView::OnLButtonUp(UINT nFlags, CPoint point)
I am using MFC provided by the Wizard. I #include "stdafx.h" (it's
actually in afxcmn.h).

I seem to remember this error message from when I was a hotter
programmer, and it was due to something other than what it says at
face value.

Any suggestions? Or do you need more info? (Visual C++ 6.0)

-Ed P.
 
M

Mike Wahler

Ed said:
I've been away from C++ programming for awhile. Maybe this is a simple
problem. I get the error message "illegal call of non-static member
function" when I insert
DWORD hotkey;
hotkey=CHotKeyCtrl::GetHotKey();
into a Wizard-generated function that is NOT static --
void CCubesView::OnLButtonUp(UINT nFlags, CPoint point)
I am using MFC provided by the Wizard. I #include "stdafx.h" (it's
actually in afxcmn.h).

I seem to remember this error message from when I was a hotter
programmer, and it was due to something other than what it says at
face value.

No, the 'face value' meaning of the message is germane.

You have a nonstatic member function, and you're using
the syntax to call a static member function. In order
to invoke a nonstatic member function, you need to create
an object of the type which defines that function, and
call the function for that object.

e.g.

class X
{
public:
void func() const {} /* nonstatic member function */
};

int main()
{
// X::func(); /* invalid, 'func()' is nonstatic */
X obj;
obj.func(); /* OK */
// or:

obj().func(); /* OK */
return 0;
}

-Mike
 
V

Victor Bazarov

Ed said:
I've been away from C++ programming for awhile. Maybe this is a simple
problem. I get the error message "illegal call of non-static member
function" when I insert
DWORD hotkey;
hotkey=CHotKeyCtrl::GetHotKey();

If 'GetHotKey' function is non-static, and this code is not in
any member of 'CHotKeyCtrl' function, you need an object (or
a pointer to an object) of the type 'CHotKeyCtrl' and then use
operator dot (or the operator arrow) to access the function:

CHotKeyCtrl *ctrl = ??? ; // get it somehow somewhere
DWORD hotkey = ctrl->GetHotKey();
into a Wizard-generated function that is NOT static --
void CCubesView::OnLButtonUp(UINT nFlags, CPoint point)

Is CCubesView a descendant of CHotKeyCtrl? It is likely that
it's not. At this point there is not enough information about
your classes to recommend anything more.
I am using MFC provided by the Wizard. I #include "stdafx.h" (it's
actually in afxcmn.h).

I seem to remember this error message from when I was a hotter
programmer, and it was due to something other than what it says at
face value.

You seem to remember incorrectly. The face falue is just fine.
You can only call static member functions using the :: notation.
Any suggestions? Or do you need more info? (Visual C++ 6.0)

It would probalby be better if you try an MFC newsgroup and
supply them with the necessary info. I stronly recommend
'microsoft.public.vc.mfc' as your next stop.

Victor
 
R

Rolf Magnus

Ed said:
I've been away from C++ programming for awhile. Maybe this is a simple
problem. I get the error message "illegal call of non-static member
function" when I insert
DWORD hotkey;
hotkey=CHotKeyCtrl::GetHotKey();
into a Wizard-generated function that is NOT static --
void CCubesView::OnLButtonUp(UINT nFlags, CPoint point)

Read the message again:

"illegal call"

So it's talking about calling a function, not defining one.

"of non-static member function"

So you're actually calling a member function "that is NOT static".
This means that the error message must be about the call to
CHotKeyCtrl::GetHotKey(). The problem is that you're calling it as if
it were a static member function, but it doesn't seem to be one. You
need to get an object of class CHotKeyCtrl and call your function on
that object.
 
E

Ed

Thanks all for the input. I knew it was something simple. By creating
an object of the class CHotKeyCtrl and then the "." to call the
function, I got it to compile.

But now it is giving a run-time assert failure related to my use of
the function, so I may be stuck giving up that idea. I just wanted to
detect when the Shift or Control keys were pressed.

I might have to go to an MFC newsgroup.

-Ed P.
 
V

Victor Bazarov

Ed said:
Thanks all for the input. I knew it was something simple. By creating
an object of the class CHotKeyCtrl and then the "." to call the
function, I got it to compile.

But now it is giving a run-time assert failure related to my use of
the function, so I may be stuck giving up that idea. I just wanted to
detect when the Shift or Control keys were pressed.

Look at GetKeyboardState function.
I might have to go to an MFC newsgroup.

May not be a bad idea.
 
E

Ed

Thanks for the tip. I got GetKeyboardState to work - it's just what I
need.

I hadn't realized the left and right shift and control keys were
distinct (160 and 161, 162 and 163).

-Ed P.
 

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

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top