Problem setting signal handler

P

pradeep

Hello again friends:

I'm now trying to get a signal handler to work. I have made the example
code below to check I can make it work, but there is a compiler error,

argument of type ‘void (MyApp::)(int)’ does not match ‘void (*)(int)’

Can anyone help?

Thanks.



#include <iostream>
#include <signal.h>

class MyApp {
public:
MyApp(void) { signal(SIGINT, Handler); }
~MyApp(void) { signal(SIGINT, SIG_DFL); }
void Handler(int) { std::cerr << "test\n"; }
};

void main()
{
MyApp x;
sleep(10);
return 0;
}
 
M

Martin Ambuhl

pradeep said:
Hello again friends:

I'm now trying to get a signal handler to work. I have made the example
code below to check I can make it work, but there is a compiler error,

argument of type ‘void (MyApp::)(int)’ does not match ‘void (*)(int)’
^^^^^^^^

How many times do you need to be told that the language of
<is C. Why do you keep posting some other language
here?
 
A

Antoninus Twink

I'm now trying to get a signal handler to work. I have made the example
code below to check I can make it work, but there is a compiler error, [snip]
class MyApp {
public:
MyApp(void) { signal(SIGINT, Handler); }
~MyApp(void) { signal(SIGINT, SIG_DFL); }
void Handler(int) { std::cerr << "test\n"; }
};

The problem is that non-static member functions take an extra hidden
parameter (namely the "this" pointer to the current instance of the
class), so you can't use a pointer to a non-static member function as a
general pointer to a function.

The easiest solution for you will be to make the Handler function
static.
 
S

santosh

pradeep said:
Hello again friends:

I'm now trying to get a signal handler to work. I have made the
example code below to check I can make it work, but there is a
compiler error,

argument of type ?void (MyApp::)(int)? does not match ?void (*)(int)?

Can anyone help?
Thanks.

#include <iostream>
#include <signal.h>

<snip>

Please post to <It should be carried on virtually
all Usenet servers including the one you seem to be using. A program
using C that is a proper subset of C++ is topical in c.l.c++, but C++
is not topical here.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top