【问题标题】:Why does this compile in C but not C++ (sigaction)?为什么这在 C 中编译而不是 C++(sigaction)?
【发布时间】:2009-04-20 03:46:27
【问题描述】:

尝试使用 g++ 编译以下代码时出现以下错误。当我使用 gcc 编译它时,它工作正常(除了一些警告)。任何帮助表示赞赏。

g++ ush7.cpp
ush7.cpp: In function ‘int signalsetup(sigaction*, sigset_t*, void (*)(int))’:
ush7.cpp:93: error: expected unqualified-id before ‘catch’
ush7.cpp:95: error: expected primary-expression before ‘catch’
ush7.cpp:95: error: expected `;' before ‘catch’
ush7.cpp:97: error: expected primary-expression before ‘catch’
ush7.cpp:97: error: expected `;' before ‘catch’
ush7.cpp:100: error: expected primary-expression before ‘catch’
ush7.cpp:100: error: expected `)' before ‘catch’
ush7.cpp:108: error: expected `)' before ‘;’ token
ush7.cpp:108: error: expected `)' before ‘;’ token
ush7.cpp: In function ‘int makeargv(const char*, const char*, char***)’:
ush7.cpp:137: error: invalid conversion from ‘void*’ to ‘char*’
ush7.cpp:145: error: invalid conversion from ‘void*’ to ‘char**’

int signalsetup(struct sigaction *def, sigset_t *mask, void (*handler)(int))
{
   struct sigaction catch;

   catch.sa_handler = handler;  /* Set up signal structures  */
   def->sa_handler = SIG_DFL;
   catch.sa_flags = 0;
   def->sa_flags = 0;
   if ((sigemptyset(&(def->sa_mask)) == -1) ||
       (sigemptyset(&(catch.sa_mask)) == -1) ||
       (sigaddset(&(catch.sa_mask), SIGINT) == -1) ||
       (sigaddset(&(catch.sa_mask), SIGQUIT) == -1) ||
       (sigaction(SIGINT, &catch, NULL) == -1) ||
       (sigaction(SIGQUIT, &catch, NULL) == -1) ||
       (sigemptyset(mask) == -1) ||
       (sigaddset(mask, SIGINT) == -1) ||
       (sigaddset(mask, SIGQUIT) == -1))
       return -1;
    return 0;
}

【问题讨论】:

    标签: c++ c g++


    【解决方案1】:

    catch 在 C++ 中是关键字,但在 C 中不是。

    请看我的相关回答 C 不是 C++ 的真子集here,甚至更好here

    【讨论】:

    • 您的其他帖子帮助我解决了使用 malloc 时遇到的其他错误。
    【解决方案2】:

    如果您不想更改其余代码,您应该可以使用#define。

    #define catch _catch
    

    【讨论】:

    • 感谢您的建设。幸运的是,只有这一个函数有几个调用:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2019-12-12
    • 2010-12-18
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多