Signal Handling in C++

S

san

Greetings,

I have a C++ problem. I am implementing a signal handler inside a class
SignalClass. The code looks as follows:
SignalClass is an Singleton class.

class SignalClass {
private:
int assign(int sig);
void handler (int sig);

};

void SignalClass::handler (int sig) {
cout << sig << endl;
}

int SignalClass::assign(int sig)
{
sigaction sa;
sa.sa_handler = handler; // ERROR at this line
.....
}

I am getting the compiler error as "Cannot assign
void(SignalClass::*)(int) to extern "C" void(*)(int)"

Any help in solving this problem is much appreciated.

Thanks in advance.
 
S

Sharad Kala

[snip]

| I have a C++ problem. I am implementing a signal handler inside a class
| SignalClass. The code looks as follows:
| SignalClass is an Singleton class.
| I am getting the compiler error as "Cannot assign
| void(SignalClass::*)(int) to extern "C" void(*)(int)"

Type of pointer to function is different from pointer to a member function,
hence the error.

| Any help in solving this problem is much appreciated.

Check this FAQ --
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2

Sharad
 
B

benben

san said:
Greetings,

I have a C++ problem. I am implementing a signal handler inside a class
SignalClass. The code looks as follows:
SignalClass is an Singleton class.

class SignalClass {
private:
int assign(int sig);
void handler (int sig);

};

void SignalClass::handler (int sig) {
cout << sig << endl;
}

int SignalClass::assign(int sig)
{
sigaction sa;
sa.sa_handler = handler; // ERROR at this line
....
}

I am getting the compiler error as "Cannot assign
void(SignalClass::*)(int) to extern "C" void(*)(int)"

Any help in solving this problem is much appreciated.

Thanks in advance.

What is sigaction? And how is its member sigaction::sa_handler declared?

If I guess right sigaction::sa_handler is a pointer to a function while
in the line of error handler is a (pointer to a) member function.
Consider declaring sigaction::sa_handler as

class sigaction{
public:
void (SignalClass::*(sa_handler))(int);
};

Or changing SignalClass::handler as:

class SignalClass{
static void handler(int);
};

Regards,
Ben
 
C

Clem.Dickey

If these are signals on the C (or POSIX) sense, make sure that you
follow the C++ or POSIX restrictions, respectively, on signal handers.
In particular, a C++ signal must be a Plain Old Function (not a member
function), and use of std::cout (which is not of typt sigatomic_t) as a
non-const will cause its value to become undefined.

This link has more details:
http://www.thescripts.com/forum/thread138191.html
 
L

Larry I Smith

san said:
Greetings,

I have a C++ problem. I am implementing a signal handler inside a class
SignalClass. The code looks as follows:
SignalClass is an Singleton class.

class SignalClass {
private:
int assign(int sig);
void handler (int sig);

};

void SignalClass::handler (int sig) {
cout << sig << endl;
}

int SignalClass::assign(int sig)
{
sigaction sa;
sa.sa_handler = handler; // ERROR at this line
....
}

I am getting the compiler error as "Cannot assign
void(SignalClass::*)(int) to extern "C" void(*)(int)"

Any help in solving this problem is much appreciated.

Thanks in advance.

Signal Handler functions must be stand-alone 'C' functions
(i.e. not members of a class). If declared in a C++ file
they must be declared 'extern "C"'.

Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top