Solved

Cannot declare member function to have static linkage - -HELP!

Posted on 2004-09-20
11
8,053 Views
Last Modified: 2012-06-21
I am trying to catch a signal and am having problems with the handler.  I declared a static function to handle the signal, but I keep on getting this error:

"Cannot declare member function 'void SignalThread::messageHandler(int) to have static linkage.  
(This class inherits from QThread)

Here is the some of the code:

void SignalThread::run()
{
  for (;;)
  {
    signalServerFlag = 0;
    signal(SIGUSR1, messageHandler);
  }
}
static void SignalThread::messageHandler(int signum)
{
    signalServerFlag = 1;
    window->show();
    return;
}


0
Comment
Question by:jewee
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12102531
If the message handler is not static in the mase class, you can't make it static in a derived class.

Check out the generic solution at http://www.codeproject.com/win32/callback_adapter.asp ("Use member functions for C-style callbacks and threads - a general solution"), this applies also for non-windows programs.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 12102560
If the messageHandler function is not a member of the base class, the "static" keyword needs to be in the class declaration, and not in the definition:

class SignalThread : public QThread
{
...
static void messageHandler(int signum);
...
};

And then in the cpp file:
void SignalThread::messageHandler(int signum)
{
}

0
 
LVL 13

Expert Comment

by:SteH
ID: 12102610
And another hint:
You call window->show ();
But window is not a local variable. If it is a member of SignalThread it must be static as well be accessible from a static function.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 12103243
Example code posted in new associated question:

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 30

Expert Comment

by:Axter
ID: 12103248
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12103922
Or that ;-)

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21137712.html

Replace 'MyThread' by 'SignalThread' and 'signalHandler' by 'messageHandler'

Regards, Alex
0
 
LVL 30

Expert Comment

by:Axter
ID: 12104231
itsmeandnobodyelse,
FYI:
I always use the short version of the links, not because I want to save typeing, but because if you click on the short version, you still get the same EE look that you're already in.

Example:
Old-Look
http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/Cplusplus/Q_21137645.html

New-Look
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21137645.html

Your-current-look
http:Q_21137645.html

Since I realy, realy, realy hate the new look, I post the short version, so as not to force the reader into a look they don't want.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12104490
@axter

it wasn't done purposely as i didn't know there is an old look.

how do i get a short link? Is there any accelerator or do i have to delete the path? And how to change to the old look?

Regards, Alex
0
 
LVL 30

Expert Comment

by:Axter
ID: 12105121
>>it wasn't done purposely as i didn't know there is an old look.
I didn't think you did it intentional, that's why I posted it as an FYI :-)

>>how do i get a short link?
Just posted exactly as you see in the link I posted. (http:????????ID.html)


>>And how to change to the old look?

Just replace www to oldlook.

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus
to
http://oldlook.experts-exchange.com/Programming/Programming_Languages/Cplusplus

The one bad thing about the oldlook, is that when you get an email notification, the email notification has links to the new look.

Also, recently, it looks like the search engine has been disabled for the oldlook.  I'm not sure if that's a temporary bug, or permanent.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12105307
@axter:

Thanks. I'll give it a try.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 109
Grammars for C C++ and java 1 123
Unresolved External Symbols 3 86
Why  my code (program) build with old compiler? 11 49
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 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