jewee
asked on
Problem with member in static member function
Question:
In my header file, I have the following pointer defined....
WinForm *winForm....
I pass in a pointer within my constructor:
SpecThread::SpecThread(Win Form *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
In my header file, I have the following pointer defined....
WinForm *winForm....
I pass in a pointer within my constructor:
SpecThread::SpecThread(Win
{
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
If you're trying to access the modified pointer from the calling function, then you need to pass a reference to the pointer.
SpecThread::SpecThread(Win Form& *winForm) //Reference to the pointer
{
this->winForm = winForm;
}
{
this->winForm = winForm;
}
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.
>>Then I try to use it in a static method........
You can not access a non-static member from a static member directly.
>>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.
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.
ASKER
Didn't work. It states that it cannot declare pointers to references..
>>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.
As I stated in my follow-up comments, you can not access none-static members from a static member function directly.
ASKER
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??
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.
Your static function can then get a pointer to then instance via the ID.
Have you read the proposals in your other Q?
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::~SignalThrea d()
{
MyList.erase(My_signum);
}
std::map<int,SignalThread* > SignalThread::MyList;
void SignalThread::messageHandl er(int signum)
{
SignalThread* window = MyList[signum];
// signalServerFlag = 1;
window->show();
return;
}
int main(){
SignalThread SignalThread1(100);
SignalThread SignalThread2(22);
SignalThread::messageHandl er(22);
SignalThread::messageHandl er(100);
return 0;
}
class SignalThread
{
public:
SignalThread(int signum);
~SignalThread();
static void messageHandler(int signum);
void show(){}
private:
static std::map<int,SignalThread*
int My_signum;
};
SignalThread::SignalThread
{
My_signum = signum;
MyList[My_signum] = this;
}
SignalThread::~SignalThrea
{
MyList.erase(My_signum);
}
std::map<int,SignalThread*
void SignalThread::messageHandl
{
SignalThread* window = MyList[signum];
// signalServerFlag = 1;
window->show();
return;
}
int main(){
SignalThread SignalThread1(100);
SignalThread SignalThread2(22);
SignalThread::messageHandl
SignalThread::messageHandl
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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::thre adFunc, 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
void WinForm::createThread(...)
{
// pass this pointer to thread
beginThread(MyThread::thre
// 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*
{
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
>>>> 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
That's a typo, it should be
>>>> m_pWinForm = (WinForm*)pParam; // store the window pointer
Regards, Alex
ASKER
Now I am getting a ld: fatal symbol referencing error..
undefined symboll, first referenced in file SignalThread..
undefined symboll, first referenced in file SignalThread..
Can you post the code you have now?
ASKER
#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::messageHandl er (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_S IZE");
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(&mainWi n);
sigThread->start();
return app.exec();
}
#include "SignalThread.h"
SignalThread::SignalThread
{
this->mainWin = mainWin;
}
void SignalThread::run()
{
for (;;)
{
signal(SIGUSR1, messageHandler);
}
}
//message handler method is static.
void SignalThread::messageHandl
{
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_S
if (fontSizeStr)
fontSize = atoi(fontSizeStr);
// Create main window
MainForm mainWin;
// Connect to Server
mainWin.connectServer(argc
// Show Main window
app.setMainWidget(&mainWin
mainWin.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
// Begin signal thread
SignalThread*sigThread = new SUBUI_signalThread(&mainWi
sigThread->start();
return app.exec();
}
ASKER
oops typo....
In the main, I meant......
// Begin signal thread
SignalThread*sigThread = new SignalThread(&mainWin);
sigThread->start();
In the main, I meant......
// Begin signal thread
SignalThread*sigThread = new SignalThread(&mainWin);
sigThread->start();
>>jewee,
Where is the class declaration for type MainForm?
Where is the class declaration for type MainForm?
ASKER
I am all set. It was something else entirely differently. Thank you for your feedback.
ASKER
PLEASE CLOSE OUT THIS QUESTION...