Evan
Evan

Reputation: 23

A pointer-to-member is not valid for a managed class

I have a Windows Form in Visual Studio C++. (CLR)

In the header file I declare bool isRunning (to find if notepad is running):

private: 
    bool isRunning(LPCSTR pnotepad)
    {
        HWND hwnd;
        hwnd = FindWindow(NULL, pnotepad);
        if (hwnd != 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Now on a checkbox, I want it to check if the process is running.

private: System::Void checkBox2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {

if (bool application::GUI::isRunning)
    label1->Text = "cat";

I get this error:

a pointer-to-member is not valid for a managed class

I tried changing it to &isRunning. This gives me the same error as above and

illegal operation on bound member function expression

How can I fix this?

Upvotes: 1

Views: 2685

Answers (2)

seleciii44
seleciii44

Reputation: 1569

everything with below seems wrong:

if (bool application::GUI::isRunning)
  1. you don't need bool if you don't wanna save the result of function. Either define a variable that's bool and assign the result of function to that:

    bool result = isRunning(...);
    if(result)
         ...
    

or

if(isRunning())
   ...
  1. application::gui::isRunning expression returns the pointer of isRunning function which you are trying to define as a bool variable.
  2. Lets say you fixed first two as:

    LPCSTR arg = ...;
    if(application::GUI::isRunning(arg))
         label1->Text = "cat";
    

Which implies that you are calling static function of a GUI class or a function under the namespace of GUI (also GUI is under application namespace).

My guess is GUI is a Form class so you are trying to invoke and since the function is not static you will get error again. So you have two cases to fix:

if you are getting this error from another function of GUI

 LPCSTR arg = ...;
 if (isRunning(arg))
     label1->Text = "cat";

otherwse you need a pointer to GUI object:

 LPCSTR arg = ...;
 if (gui-> isRunning(arg))
     label1->Text = "cat";

Upvotes: 2

Pravar Jawalekar
Pravar Jawalekar

Reputation: 605

I think you are making function call in incorrect manner. Probably it should be like below,

if (application::GUI::isRunning())
{
    label1->Text = "cat";
}

Above is just a hint to make proper function call - but since isRunning is a private member function, how it can be invoked directly from outside class and that too without creating any object. It is not a static member function. Please check this point.

Upvotes: 0

Related Questions