-2

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.

2
  • If you're new in C++, you should read about how static variables are initialized in a C++ class. Any static variable must be explicitly initialized in the respective cpp source file... Commented Dec 4, 2014 at 15:39
  • stackoverflow.com/questions/743203/… Commented Dec 4, 2014 at 15:39

2 Answers 2

0

You have to define the static array. Inside the class definition it is only declared. Write in the cpp module

EventHandler * SignalHandler::signalHandlers[NSIG]; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer it really works. Can you give any good article to read about this. Why this should be defined both in header and source files .
@braohaufngec I am sorry. I do not know an appropriate article. It is better to read the C++ Standard.
0

Every function and object which is ODR-used must be defined.

And static SignalHandler *instance; in class-scope only declares that member, it does not define it.

Add the definition to exactly one translation-unit (.cpp-file).

SignalHandler* Signalhandler::instance; 

Same for all the other static members.

2 Comments

Thanks for answer it really works. Can you give any good article to read about this. Why this should be defined both in header and source files .
@braohaufngec: It should not be defined in both. The line in the class-definition is only a forward-declaration, which is only a promise for a definition somewhere else, like in another translation-unit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.