I'm sick of this, I'm really exhausted. I am new in C++. But I have read a lot about that ,but still doesn't understand what is the correct to implement this.
I have just copied all code from this article Signal Handling in C++
BUT it still doesn't work !!!! I created new class Signal Handler.... Copy everything from this article BUT still this .... error message "Undefined reference .......". I am really confused about this. Please explain how to do this correctly and why I always get this message.
Here is just code copied from the article.
SignalHandler.h
#ifndef SIGNALHANDLER_H
#define SIGNALHANDLER_H
#include "eventhandler.h"
#include <signal.h>
class SignalHandler
{
public:
// Entry point.
static SignalHandler *getInstance (void);
// Register an event handler <eh> for <signum>
// and return a pointer to any existing <Event_Handler>
// that was previously registered to handle <signum>.
EventHandler* registerHandler (int signum,EventHandler *eh);
// Remove the <Event_Handler> for <signum>
// by setting the slot in the <signal_handlers_>
// table to NULL.
int removeHandler (int signum);
private:
// Ensure we're a Singleton.
SignalHandler (void);
// Singleton pointer.
static SignalHandler *instance;
// Entry point adapter installed into <sigaction>
// (must be a static method or a stand-alone
// extern "C" function).
static void dispatcher (int signum);
// Table of pointers to concrete <Event_Handler>s
// registered by applications. NSIG is the number of
// signals defined in </usr/include/sys/signal.h>.
static EventHandler *signalHandlers[NSIG];
};
#endif // SIGNALHANDLER_H
SignalHandler.cpp
#include "signalhandler.h"
EventHandler* SignalHandler::registerHandler (int signum,EventHandler *eh)
{
// Copy the <old_eh> from the <signum> slot in
// the <signal_handlers_> table.
EventHandler *oldEh = SignalHandler::signalHandlers[signum];
// Store <eh> into the <signum> slot in the
// <signal_handlers_> table.
SignalHandler::signalHandlers[signum] = eh;
// Register the <dispatcher> to handle this
// <signum>.
struct sigaction sa;
sa.sa_handler = SignalHandler::dispatcher;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sigaction (signum, &sa, 0);
return oldEh;
}
void SignalHandler::dispatcher (int signum)
{
// Perform a sanity check...
if (SignalHandler::signalHandlers[signum] != 0) {
// Dispatch the handler's hook method.
SignalHandler::signalHandlers[signum]->handleSignal(signum);
}
}
WHY am I getting this messages ...............
error: undefined reference to `SignalHandler::signalHandlers'
error: undefined reference to `SignalHandler::signalHandlers'
Note that I have just copied this from the article. I really hope for your help.Thanks.