我正在尝试将 C 文件转换为 C++ 文件,但我一直遇到以下 typedef 定义的问题。下面我展示了 Ah、A.cpp 和我的主类的代码和我的类结构。当我尝试编译时,出现以下错误:
main.cpp|44|error: no matching function for call to ‘A::Signal(int, <unresolved overloaded function type>)’|
main.cpp|44|note: candidate is:|
A.h|115|note: void (* A::Signal(int, void (*)(int)))(int)|
A.h|115|note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘void (*)(int)’|
//啊
class A
{
public:
void sigquit_handler (int sig);
typedef void handler_t(int);
handler_t *Signal(int signum, handler_t *handler);
}
//A.cpp
/*
* Signal - wrapper for the sigaction function
*/
A::handler_t* A::Signal(int signum, A::handler_t *handler) {
struct sigaction action, old_action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask); /* block sigs of type being handled */
action.sa_flags = SA_RESTART; /* restart syscalls if possible */
if (sigaction(signum, &action, &old_action) < 0) {
unix_error("Signal error");
}
return (old_action.sa_handler);
}
/*
* sigquit_handler - The driver program can gracefully terminate the
* child shell by sending it a SIGQUIT signal.
*/
void A::sigquit_handler(int sig) {
if (verbose)
printf("siquit_handler: terminating after SIGQUIT signal\n");
exit(1);
}
//main.cpp
int main(int argc, char **argv) {
A a;
a.Signal(SIGQUIT, a.sigquit_handler); /* so parent can cleanly terminate child*/
}
有人可以向我解释为什么会这样吗?我认为问题在于 sigquit_handler(void) 的返回类型和 Signal (int, handler_t*) 的输入参数,但我不明白为什么。
按照建议进行编辑:
//啊
class A
{
public:
void sigquit_handler (int sig);
typedef void (*handler_t)(int);
handler_t Signal(int,handler_t);
}
//A.cpp
/*
* Signal - wrapper for the sigaction function
*/
handler_t A::Signal(int signum, handler_t handler){
struct sigaction action, old_action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask); /* block sigs of type being handled */
action.sa_flags = SA_RESTART; /* restart syscalls if possible */
if (sigaction(signum, &action, &old_action) < 0) {
unix_error("Signal error");
}
return (old_action.sa_handler);
}
/*
* sigquit_handler - The driver program can gracefully terminate the
* child shell by sending it a SIGQUIT signal.
*/
void A::sigquit_handler(int) {
if (verbose)
printf("siquit_handler: terminating after SIGQUIT signal\n");
exit(1);
}
//main.cpp
int main(int argc, char **argv) {
A a;
a.Signal(SIGQUIT, &sigquit_handler); /* so parent can cleanly terminate child*/
}
错误:
main.cpp|44|error: ‘sigquit_handler’ was not declared in this scope|