C++(1):error: invalid conversion from ‘void (*)()’ to ‘void (*)(int)
void signaldemo_test(void) { struct itimerval tv, otv; signal(SIGALRM, sigFunc); //how long to run the first time tv.it_value.tv_sec = 3; tv.it_value.tv_usec = 0; //after the first time, how long to run next time tv.it_interval.tv_sec = 5; tv.it_interval.tv_usec = 0; if (setitimer(ITIMER_REAL, &tv, &otv) != 0) //定时每隔5s执行一次 { printf("setitimer err %d\n", errno); } while(1) { sleep(2); //隔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 = 0; 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 = 0; printf("The %d Times: my girl maybe in SZ library \n", iCnt++);} }