Problem setting signal handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pradeep

    Problem setting signal handler

    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;
    }
  • Martin Ambuhl

    #2
    Re: Problem setting signal handler

    pradeep wrote:
    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
    <news:comp.lang .cis C. Why do you keep posting some other language
    here?

    Comment

    • Antoninus Twink

      #3
      Re: Problem setting signal handler

      On 26 Apr 2008 at 21:41, pradeep wrote:
      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.

      Comment

      • santosh

        #4
        Re: Problem setting signal handler

        pradeep wrote:
        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 <news:comp.lang .c++>. 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.

        Comment

        Working...