C++(1):error: invalid conversion from ‘void (*)()’ to ‘void (*)(int)

时间:2022-08-08 12:21:35
void signaldemo_test(void)
{
struct itimerval tv, otv;
signal(SIGALRM, sigFunc);
//how long to run the first time
tv.it_value.tv_sec = ;
tv.it_value.tv_usec = ;
//after the first time, how long to run next time
tv.it_interval.tv_sec = ;
tv.it_interval.tv_usec = ;
if (setitimer(ITIMER_REAL, &tv, &otv) != ) //定时每隔5s执行一次
{
printf("setitimer err %d\n", errno);
}
while()
{
sleep();
//隔2s打印一遍
printf("otv: %d, %d, %d, %d\n", otv.it_value.tv_sec, otv.it_value.tv_usec, otv.it_interval.tv_sec, otv.it_interval.tv_sec);
}
} void sigFunc()
{
static int iCnt = ; printf("The %d Times: my girl maybe in SZ library \n", iCnt++);}
}
以上这段代码在用g++编译是会有如下错误:
/mnt/hgfs/Linux/cleanCup/src/signaldemo.cpp:9: error: invalid conversion from ‘void (*)()’ to ‘void (*)(int)’
/mnt/hgfs/Linux/cleanCup/src/signaldemo.cpp:9: error: initializing argument 2 of ‘void (* signal(int, void (*)(int)))(int)’ 值得注意的是在gcc编译时不会出错,但是用g++就会有问题,究其原因就是C语言编译器允许隐含性的将一个通用指针转换为任意类型的指针,而C++不允许。
void sigFunc(int)
{
static int iCnt = ; printf("The %d Times: my girl maybe in SZ library \n", iCnt++);}
}