[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Problem with member in static member function

Posted on 2004-09-20
21
Medium Priority
?
2,588 Views
Last Modified: 2008-01-09
Question:

In my header file, I have the following pointer defined....
WinForm *winForm....

I pass in a pointer within my constructor:

SpecThread::SpecThread(WinForm *winForm)
{
   this->winForm = winForm;
}


Then I try to use it in a static method........
winForm->displayAll();

I receive an error, invalid use of member SpecThread::winForm in static member function
0
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 2
  • +2
21 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12103009
If you're trying to access the modified pointer from the calling function, then you need to pass a reference to the pointer.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12103016
SpecThread::SpecThread(WinForm& *winForm) //Reference to the pointer
{
  this->winForm = winForm;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 12103027
Disregard previous comments.  Misread the question.

>>Then I try to use it in a static method........

You can not access a non-static member from a static member directly.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 12103046
>>I receive an error, invalid use of member SpecThread::winForm in static member function

In order to use a none-static member in a static member function, you need to have a pointer/reference to a valid instance of the object.

Exactly what are you trying to do?
We could give you some better alternatives.
0
 

Author Comment

by:jewee
ID: 12103052
Didn't work.  It states that it cannot declare pointers to references..
0
 
LVL 30

Expert Comment

by:Axter
ID: 12103067
>>Didn't work.  It states that it cannot declare pointers to references..

As I stated in my follow-up comments, you can not access none-static members from a static member function directly.
0
 

Author Comment

by:jewee
ID: 12103095
This thread class I have, the handler method has to be declared static when I try to catch a signal.  within the handler method, if the signal is caught, then I display the window.  Unless I create a non static function and call it within the static function??
0
 
LVL 30

Expert Comment

by:Axter
ID: 12103108
If each instance of your class has an assoicated unique ID number, then you could trying creating a list or map container that pointed to each instance via ID.

Your static function can then get a pointer to then instance via the ID.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12103196
Have you read the proposals in your other Q?
0
 
LVL 30

Expert Comment

by:Axter
ID: 12103209
Example:

class SignalThread
{
public:
      SignalThread(int signum);
      ~SignalThread();
      static void messageHandler(int signum);
      void show(){}
private:
      static std::map<int,SignalThread*> MyList;
      int My_signum;
};


SignalThread::SignalThread(int signum)
{
      My_signum = signum;
      MyList[My_signum] = this;
}

SignalThread::~SignalThread()
{
      MyList.erase(My_signum);
}

std::map<int,SignalThread*> SignalThread::MyList;

void SignalThread::messageHandler(int signum)
{
      SignalThread* window = MyList[signum];
//   signalServerFlag = 1;
   window->show();
   return;
}



int main(){
      SignalThread SignalThread1(100);
      SignalThread SignalThread2(22);
      SignalThread::messageHandler(22);
      SignalThread::messageHandler(100);
      return 0;
}
0
 
LVL 3

Accepted Solution

by:
aviadbd earned 2000 total points
ID: 12103250

>> Unless I create a non static function and call it within the static function??

You cannot call any non-static members, functions included, from within a static method.

If your handler is a single handler within your entire application, you could try the singleton approach - that way you could initialize it with your WinForm at the beginning of the application and call it from the static method.

class MySingleton
{
  protected:
    MySingleton();

  public:
    static MySingleton* GetInstance();
    void SetWinForm(WinForm* winForm);

  private:
    MySingleton* _instance;
}

check this out for how to implement:
http://www.codeproject.com/cpp/singletonrvs.asp

Good luck!
AviadBD.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12103815
If a worker thread has to communicate with the main thread (GUI), normally its done by passing a pointer of the active window as thread parameter:

  void WinForm::createThread(...)
  {
        // pass this pointer to thread
       
        beginThread(MyThread::threadFunc, 0, this, ...);
        // CreateThread(NULL, 0, MyThread::threadFunc, this, ...);
  }

class MyThread is declared like that:

class MyThread
{
public:
             ...
       // the one and only thread function
       static void threadFunc(void* pParam);    
       static void signalHandler(..);

private:
       static WinForm* m_pWinForm;   // pointer to window
};

  void MyThread::threadFunc(void* pParam)
  {
        m_pWinForm* = (WinForm*)pParam;    //   store the window pointer

        ....
  }

  void MyThread::signalHandler(..)
  {
        ...
        m_pWinForm->displayAll(); // that works as m_pWinForm is static also
        ...
  }

Note, if you have more than one worker thread referring to different windows the solution doesn't work.

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12103832
>>>> m_pWinForm* = (WinForm*)pParam;    //   store the window pointer

That's a typo, it should be

>>>> m_pWinForm = (WinForm*)pParam;    //   store the window pointer

Regards, Alex

       
0
 

Author Comment

by:jewee
ID: 12105510
Now I am getting a ld: fatal symbol referencing error..

undefined symboll, first referenced in file SignalThread..
0
 
LVL 30

Expert Comment

by:Axter
ID: 12105580
Can you post the code you have now?
0
 

Author Comment

by:jewee
ID: 12105688
#include.....
#include "SignalThread.h"

SignalThread::SignalThread(MainForm *mainForm)
{
   this->mainWin = mainWin;
}
void SignalThread::run()
{
   for (;;)
   {  
      signal(SIGUSR1, messageHandler);
   }
}
//message handler method is static.
void SignalThread::messageHandler (int signum)
{
    mainWin->show();
    return;
}

//in my header file, I have the following:

#ifndef  SIGNALTHREAD_H_
#define  SIGNALTHREAD_H_

#include "MainForm.h"
#include <qthread.h>

class SignalThread: public QThread
{
   public:

      SignalThread(MainForm *mainForm);

      ~SignalThread();
      virtual void run();

      static void messageHandler(int signum);
   private:
};

#endif // SIGNALTHREAD_H

In my TestMain.cpp file, i have the following...

#include <qapplication.h>
#include <qwindowsstyle.h>
#include <iostream>
#include <qthread.h>
#include "MainForm.h"
#include "SignalThread.h"

int main( int argc, char ** argv )
{
    QApplication app( argc, argv );
    // Check if environmet variable "APPLICATION_FONT_SIZE" exists
    int fontSize = 10;
    char* fontSizeStr = getenv("APPLICATION_FONT_SIZE");
    if (fontSizeStr)
      fontSize = atoi(fontSizeStr);

        // Create main window
    MainForm mainWin;

    // Connect to Server
    mainWin.connectServer(argc, argv);

    // Show Main window
    app.setMainWidget(&mainWin);
    mainWin.show();

    app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );

    // Begin signal thread
    SignalThread*sigThread = new SUBUI_signalThread(&mainWin);
    sigThread->start();
   
    return app.exec();


}
0
 

Author Comment

by:jewee
ID: 12105702
oops typo....

In the main, I meant......

// Begin signal thread
    SignalThread*sigThread = new SignalThread(&mainWin);
    sigThread->start();
0
 
LVL 30

Expert Comment

by:Axter
ID: 12106995
>>jewee,

Where is the class declaration for type MainForm?
0
 

Author Comment

by:jewee
ID: 12107085
I am all set.  It was something else entirely differently.  Thank you for your feedback.

0
 

Author Comment

by:jewee
ID: 12107087
PLEASE CLOSE OUT THIS QUESTION...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question