Fra*_*nXh 0 c c++ typedef void
我正在尝试转换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)’|
Run Code Online (Sandbox Code Playgroud)
//啊
class A
{
public:
void sigquit_handler (int sig);
typedef void handler_t(int);
handler_t *Signal(int signum, handler_t *handler);
}
Run Code Online (Sandbox Code Playgroud)
//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);
}
Run Code Online (Sandbox Code Playgroud)
//main.cpp
int main(int argc, char **argv) {
A a;
a.Signal(SIGQUIT, a.sigquit_handler); /* so parent can cleanly terminate child*/
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么会这样吗?我认为问题是因为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);
}
Run Code Online (Sandbox Code Playgroud)
//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);
}
Run Code Online (Sandbox Code Playgroud)
//main.cpp
int main(int argc, char **argv) {
A a;
a.Signal(SIGQUIT, &sigquit_handler); /* so parent can cleanly terminate child*/
}
Run Code Online (Sandbox Code Playgroud)
错误:
main.cpp|44|error: ‘sigquit_handler’ was not declared in this scope|
Run Code Online (Sandbox Code Playgroud)
我假设这个(错误的)行:
typedef void handler_t(int);
Run Code Online (Sandbox Code Playgroud)
你想要一个typedef指向函数的指针.为此,您必须添加一些括号和星号:
typedef void (*handler_t)(int);
Run Code Online (Sandbox Code Playgroud)
但你真正需要的是一个指向成员函数的指针的typedef,它是这样做的:
typedef return_type (class_type::* func_t)(arg);
Run Code Online (Sandbox Code Playgroud)
然后(见代码评论):
// Second parameter is wrong: you need to use
// A::handler_t
// instead of:
// A::handler_t*
//
// error here error here
// v v
A::handler_t* A::Signal(int signum, A::handler_t* handler)
{
struct sigaction action, old_action; // 'struct' not needed in C++
// these 2 variables are not initialized
// (except if they have a default constructor).
// ...
return (old_action.sa_handler); // parenthesis not needed. old_action was not changed in this function
}
Run Code Online (Sandbox Code Playgroud)
最后,这是如何将指针传递给成员:
A a;
a.Signal(SIGQUIT, &A::sigquit_handler);
Run Code Online (Sandbox Code Playgroud)
现在,另一个问题:sigaction::sa_handler是不是一个成员指针.所以,你需要sigquit_handler离开课堂A,并改变功能Signal:
// A.h
class A
{
public:
typedef void (*handler_t)(int);
handler_t Signal(int,handler_t);
};
void sigquit_handler(int);
// A.cpp
void sigquit_handler(int)
{
// ...
}
A::handler_t A::Signal(int,handler_t)
{
// ...
}
// main.cpp
int main()
{
A a;
a.Signal(SIGQUIT, &sigquit_handler);
}
Run Code Online (Sandbox Code Playgroud)