0

我正在尝试将 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 ‘&lt;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|
4

2 回答 2

5

我假设这个(错误的)行:

typedef void handler_t(int);

您想将 typedef 设为指向函数的指针。为此,您必须添加一些括号和星号:

typedef void (*handler_t)(int);

但是你真正需要的是一个指向成员函数的指针的 typedef,它是这样完成的:

typedef return_type (class_type::* func_t)(arg);

然后(见代码注释):

// 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
}

最后,这里是如何将指针传递给成员:

A a;
a.Signal(SIGQUIT, &A::sigquit_handler);

现在,另一个问题: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);
}
于 2013-03-17T17:44:56.560 回答
3

您在这里有几个问题,一个是您使用指向函数的指针,但将其传递给成员函数。这两个不一样。我建议你看看std::functionand std::bind

于 2013-03-17T17:46:05.720 回答