【发布时间】:2011-09-23 09:47:06
【问题描述】:
void (*)(int) 类型的参数与__sighnd64_t 类型的参数不兼容
下面是我的简单代码:
#include <iostream>
#include <string>
#include <signal.h>
#include <ctype>
#include <stdlib.h>
#include <stdio.h>
typedef struct mystrcut
{
int a;
char *b;
} mystr;
void set_string ( char **, const char * );
void my_handler(int s)
{
printf("Caught signal %d\n",s);
exit(1);
}
int main()
{
const std::string str1[] = {"hello1", "hello2"};
char str2[50];
size_t size1 = str1[1].size();
cout << size1;
memcpy (str2, str1[1].c_str(), size1);
cout << str2;
mystr *m = NULL;
m = new mystrcut;
m->a = 5;
set_string(&m->b, "hello");
cout << m->b;
delete []m->b;
// void (*prev_fn)(int);
signal (SIGINT,my_handler);
return 0;
}
void set_string ( char **a, const char *b)
{
*a = new char [strlen(b)+1];
strcpy (*a, b);
}
我正在开发 openvms。我可以通过某种类型转换来避免编译错误吗?我的编译器需要 `__sighnd64_t __64_signal(int, __sighnd64_t);
在处理程序 extern c 周围添加有效。谢谢
【问题讨论】:
-
这很奇怪,因为 AFAIK
__sighnd64_t的类型定义为void (*)(int)。但是您的代码中还有其他问题...例如cout必须是未声明的标识符,因为您不使用std:: -
您可能必须查看编译器的
文件(哪个?),以准确了解它对 signal()参数的期望。 -
抱歉这个可能很愚蠢的问题,但是你有编译问题吗?在 linux 上它可以编译和工作,在需要的地方正确添加 std::。
-
我也看不出拨打
signal有任何问题。但是他传给signal的函数有一个很严重的问题;你不能打电话给printf。 (另外,如果您只关心 Unix 机器的可移植性,您应该使用sigaction而不是signal。) -
尝试将您的处理程序声明为
extern "C"。这是严格意义上的标准要求。gcc不强制执行此操作,但其他一些编译器会执行此操作。
标签: c++ signal-handling openvms