Problem setting signal handler

Discussion in 'C Programming' started by pradeep, Apr 26, 2008.

  1. pradeep

    pradeep Guest

    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;
    }
     
    pradeep, Apr 26, 2008
    #1
    1. Advertising

  2. 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.c> is C. Why do you keep posting some other language
    here?
     
    Martin Ambuhl, Apr 26, 2008
    #2
    1. Advertising

  3. 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.
     
    Antoninus Twink, Apr 26, 2008
    #3
  4. pradeep

    santosh Guest

    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.
     
    santosh, Apr 27, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Pronath
    Replies:
    1
    Views:
    1,194
    Diez B. Roggisch
    Jan 3, 2005
  2. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    478
    Jack Orenstein
    Sep 18, 2005
  3. Weng Tianxiang
    Replies:
    2
    Views:
    671
    Jonathan Bromley
    Jan 30, 2007
  4. Nicolas Moreau
    Replies:
    9
    Views:
    3,245
  5. dibacco73
    Replies:
    1
    Views:
    667
    joris
    Feb 12, 2009
Loading...

Share This Page