Function Pointers and Error: Could not find a match

Discussion in 'C++' started by Christopher Parent, Jun 25, 2003.

  1. I have perused through the groups and a couple C++ manuals and haven't
    been able to figure this one out. Maybe I'm staring at it too much.
    Here's the setup, I have 2 files. One is a library and one is the
    program source:

    MyLibrary.C
    -----------

    int InstallSigHandler( void (*sigHandler) (int) )
    {
    // ....
    return ...
    }


    MyMain.C
    --------

    #include MyLibrary.h
    void *MySigHandler(int);

    main()
    {
    InstallSigHandler(MySigHandler); <== Error Line
    }
    void *MySigHandler(int x)
    {
    // ...
    }

    Error: Could not find a match for InstallSigHandler( void*(int)).
    --------------------

    The library, MyLibrary, compiles without warnings or errors and is
    successfully included when I compile MyMain.C. However, I receive the
    error above when compiling MyMain.C.

    I feel like there should be parentheses around the void* in the error
    message. Not sure. But if anyone out there could lend some advice,
    thanks.

    Chris
     
    Christopher Parent, Jun 25, 2003
    #1
    1. Advertising

  2. Christopher Parent

    MiniDisc_2k2 Guest

    "Christopher Parent" <> wrote in message
    news:...
    > I have perused through the groups and a couple C++ manuals and haven't
    > been able to figure this one out. Maybe I'm staring at it too much.
    > Here's the setup, I have 2 files. One is a library and one is the
    > program source:
    >
    > MyLibrary.C
    > -----------
    >
    > int InstallSigHandler( void (*sigHandler) (int) )
    > {
    > // ....
    > return ...
    > }
    >
    >
    > MyMain.C
    > --------
    >
    > #include MyLibrary.h
    > void *MySigHandler(int);


    Looks to me like this returns a pointer to void, not what you intended, the
    address of a function which returns void? Try this

    void (*MySigHandler)(int)

    >
    > main()
    > {
    > InstallSigHandler(MySigHandler); <== Error Line


    or alternatively change it here:
    InstallSigHandler(&MySigHandler);
    and get rid of the * in the function declaration above.

    > }
    > void *MySigHandler(int x)


    Change this to match the function declaration.

    > {
    > // ...
    > }
    >
    > Error: Could not find a match for InstallSigHandler( void*(int)).

    This indicates that it's looking for a function which will accept a function
    to return a void*. Not what you intended.
    ----------
    >
    > The library, MyLibrary, compiles without warnings or errors and is
    > successfully included when I compile MyMain.C. However, I receive the
    > error above when compiling MyMain.C.
    >
    > I feel like there should be parentheses around the void* in the error
    > message.


    Yup. See above.

    > Not sure.


    No seriously, you're right.

    > But if anyone out there could lend some advice,
    > thanks.


    No problem.

    >
    > Chris


    -- MiniDisc_2k2
    To reply, replace nospam.com with cox dot net
     
    MiniDisc_2k2, Jun 25, 2003
    #2
    1. Advertising

  3. "Christopher Parent" <> wrote...
    > I have perused through the groups and a couple C++ manuals and haven't
    > been able to figure this one out. Maybe I'm staring at it too much.
    > Here's the setup, I have 2 files. One is a library and one is the
    > program source:
    >
    > MyLibrary.C

    ^^^

    > -----------
    >
    > int InstallSigHandler( void (*sigHandler) (int) )
    > {
    > // ....
    > return ...
    > }
    >
    >
    > MyMain.C
    > --------
    >
    > #include MyLibrary.h

    ^^^^
    Are you sure that 'InstallSigHanlder' is correctly _declared_ in
    that header? Since you didn't post it we have no way to verify
    that.

    > void *MySigHandler(int);
    >
    > main()


    Should be

    int main()

    > {
    > InstallSigHandler(MySigHandler); <== Error Line
    > }
    > void *MySigHandler(int x)
    > {
    > // ...
    > }
    >
    > Error: Could not find a match for InstallSigHandler( void*(int)).
    > --------------------
    >
    > The library, MyLibrary, compiles without warnings or errors and is
    > successfully included when I compile MyMain.C. However, I receive the
    > error above when compiling MyMain.C.


    Is that error during compilation or during linking? If during
    compilation, make sure the declaration of 'InstallSigHandler' is
    visible when 'MyMain.C' is compiled. If it's during linking,
    then, I am sorry, the library must be either built wrong or with
    different settings (like for C, not C++, linkage).

    >
    > I feel like there should be parentheses around the void* in the error
    > message.


    We can't fix that, and you have no control over that. It's what
    the compiler manufacturer made it report.

    > Not sure. But if anyone out there could lend some advice,
    > thanks.


    I think you might want to post more code (like "MyLibrary.h", for
    example), and perhaps think of posting to your compiler newsgroup,
    they know more about compiler settings and how to make your libs
    to link correctly.

    Victor
     
    Victor Bazarov, Jun 25, 2003
    #3
  4. "Tom" <> wrote...
    > Lib:
    > int InstallSigHandler(void (sigHandler)(int))


    (a) This should be

    int InstallSigHandler(void (*sigHandler)(int))

    > {
    > ...
    > }
    >
    > App:
    > void MySigHandler(int);
    >
    > void main (void)
    > {
    > InstallSigHandler(&MySigHandler);


    (b) The '&' is unnecessary.

    > }
    >
    > void MySigHandler(int x)
    > {
    > ...
    > }
    >
    > This should work!


    With the correction (a) it will.

    >
    > Regards,
    > Tom
    >
    > "Christopher Parent" <> wrote in message
    > news:...
    > > I have perused through the groups and a couple C++ manuals and haven't
    > > been able to figure this one out. Maybe I'm staring at it too much.
    > > Here's the setup, I have 2 files. One is a library and one is the
    > > program source:
    > >
    > > MyLibrary.C
    > > -----------
    > >
    > > int InstallSigHandler( void (*sigHandler) (int) )
    > > {
    > > // ....
    > > return ...
    > > }
    > >
    > >
    > > MyMain.C
    > > --------
    > >
    > > #include MyLibrary.h
    > > void *MySigHandler(int);
    > >
    > > main()
    > > {
    > > InstallSigHandler(MySigHandler); <== Error Line
    > > }
    > > void *MySigHandler(int x)
    > > {
    > > // ...
    > > }
    > >
    > > Error: Could not find a match for InstallSigHandler( void*(int)).
    > > --------------------
    > >
    > > The library, MyLibrary, compiles without warnings or errors and is
    > > successfully included when I compile MyMain.C. However, I receive the
    > > error above when compiling MyMain.C.
    > >
    > > I feel like there should be parentheses around the void* in the error
    > > message. Not sure. But if anyone out there could lend some advice,
    > > thanks.
    > >
    > > Chris

    >
    >
     
    Victor Bazarov, Jun 25, 2003
    #4
  5. Christopher Parent

    MiniDisc_2k2 Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > "Tom" <> wrote...
    > > Lib:
    > > int InstallSigHandler(void (sigHandler)(int))

    >
    > (a) This should be
    >
    > int InstallSigHandler(void (*sigHandler)(int))


    Just what I was going to say...
    >
    > > {
    > > ...
    > > }
    > >
    > > App:
    > > void MySigHandler(int);
    > >
    > > void main (void)
    > > {
    > > InstallSigHandler(&MySigHandler);

    >
    > (b) The '&' is unnecessary.


    But it helps maintain readability.
     
    MiniDisc_2k2, Jun 25, 2003
    #5
  6. Victor Bazarov wrote:
    [...]
    > > App:
    > > void MySigHandler(int);
    > >
    > > void main (void)
    > > {
    > > InstallSigHandler(&MySigHandler);

    >
    > (b) The '&' is unnecessary.


    (c) http://groups.google.com/groups?selm=

    regards,
    alexander.
     
    Alexander Terekhov, Jun 25, 2003
    #6
  7. "Christopher Parent" <> wrote...
    > Alright, I've made some changes, and I know longer get any compilation
    > errors,


    You don't? Your compiler must be more forgiving than mine.

    > but now I'm getting a link error. Here's what I've done:
    >
    > MyLibrary.C/h (This is the same as before)
    > -----------
    >
    > int InstallSigHandler( void (*sigHandler) (int) )
    > {
    > ...;
    > }
    >
    > MyProgram.C
    > -----------
    >
    > #include MyLibrary.h


    #include <MyLibrary.h>

    > void MySigHandler(int);
    >
    > main()


    int main()

    > {
    > InstallSigHandler(MySigHandler);
    > }
    >
    > void MySigHandler(int x)
    > {
    > // ...
    > }
    >
    > The linkage error that I'm getting is
    >
    > Undefined symbol first referenced in file
    > int InstallSigHandler(void(*)(int)) MyProgram.o


    Are you sure you're including all compiled files at the linking
    stage of making the program?
     
    Victor Bazarov, Jun 26, 2003
    #7
  8. Christopher Parent

    MiniDisc_2k2 Guest

    Ensure that the compiler is actually compiling both files (and that the
    linker is linking the two together). Perhaps you could combine them into one
    file?
    -- MiniDisc_2k2

    To reply, replace nospam.com with cox dot net
     
    MiniDisc_2k2, Jun 26, 2003
    #8
  9. Christopher Parent

    Ron Natalie Guest


    > void MySigHandler(int);


    Do you want
    extern "C" void MySigHandler(int);

    >
    > main()


    must provide return value (int) for main.
     
    Ron Natalie, Jun 26, 2003
    #9
    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. Roxanne
    Replies:
    0
    Views:
    1,239
    Roxanne
    Jul 4, 2003
  2. TC
    Replies:
    3
    Views:
    26,176
    jessica
    May 19, 2004
  3. Adam Knight
    Replies:
    1
    Views:
    1,133
    Ken Cox - Microsoft MVP
    Dec 5, 2005
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    688
  5. E. Kwong
    Replies:
    0
    Views:
    308
    E. Kwong
    Nov 6, 2006
Loading...

Share This Page