Function Pointers and Error: Could not find a match

C

Christopher Parent

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
 
M

MiniDisc_2k2

Christopher Parent said:
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

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.

-- MiniDisc_2k2
To reply, replace nospam.com with cox dot net
 
V

Victor Bazarov

Christopher Parent said:
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
^^^^
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
 
V

Victor Bazarov

Tom said:
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.
 
V

Victor Bazarov

Christopher Parent said:
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 said:
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?
 
M

MiniDisc_2k2

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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top