Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的信号处理+;_C++_Signal Handling_Openvms - Fatal编程技术网

C++ C+中的信号处理+;

C++ C+中的信号处理+;,c++,signal-handling,openvms,C++,Signal Handling,Openvms,void(*)(int)类型的参数与类型的参数不兼容 下面是我的简单代码: #include <iostream> #include <string> #include <signal.h> #include <ctype> #include <stdlib.h> #include <stdio.h> typedef struct mystrcut { int a; char *b; } mystr; void

void(*)(int)
类型的参数与
类型的参数不兼容

下面是我的简单代码:

#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);
}
#包括
#包括
#包括
#包括
#包括
#包括
typedef结构mystrcut
{
INTA;
char*b;
}mystr;
无效集_字符串(字符**,常量字符*);
作废我的_处理程序(int s)
{
printf(“捕获的信号%d\n”,s);
出口(1);
}
int main()
{
常量std::字符串str1[]={“hello1”,“hello2”};
char-str2[50];
size_t size1=str1[1]。size();
可以说“你好”;
cout b;
删除[]m->b;
//无效(*上一页)(内部);
信号(SIGINT,我的处理器);
返回0;
}
无效集_字符串(字符**a,常量字符*b)
{
*a=新字符[strlen(b)+1];
strcpy(*a,b);
}
我正在openvms上工作。我可以通过某种类型转换来避免编译错误吗?我的编译器需要“\uuu signd 64\u t\uu 64\u信号(int,\uu signd 64\u t)


在处理程序外部添加c已经奏效。感谢

< P>信号处理不是C++,而是C。这个错误有点奇怪……/P> 在这种情况下,尝试在处理程序周围使用extern“C”(将其定义为C函数),如注释中所述。来自openvms的现代
中已经包含外部“C”:

HP仅约C,不为C++。

表示信号处理程序(catcher)必须在C中声明为

 void func(int signo);

这很奇怪,因为AFAIK
\u signd 64\u t
的类型定义为
void(*)(int)
。但是你的代码中还有其他问题。。。例如,
cout
必须是一个未声明的标识符,因为您没有使用
std::
您可能需要在文件中查找编译器(哪一个?),以查看它对
signal()
参数的确切预期。很抱歉,这个问题可能很愚蠢,但您有编译问题吗?在linux上,它编译并工作,在需要的地方正确地添加std::我也看不到调用
signal
有任何问题。但是他传递给
信号的函数有一个非常严重的问题;您不允许调用
printf
。(另外,如果您考虑的可移植性问题仅包括Unix计算机,则应使用
sigaction
而不是
signal
)尝试将处理程序声明为
extern“C”
。严格来说,这是标准要求的
gcc
不强制执行这一点,但其他一些编译器也强制执行。我认为您根本不需要将函数声明为
extern“C”
,以便从C代码调用它们。我认为您只需要在C代码中按名称引用它们时执行此操作。由于正在传递指向信号处理程序的指针,这意味着它不需要声明为
extern“C”
@Sam:这很有趣。我想知道要使用的函数调用约定是否是您系统上类型的一部分。@Omnifarious:这在标准语言中称为“语言链接”,并且它是类型的一部分(具有不同语言链接的两个函数类型是不同的类型,即使它们在其他方面相同--C++03 7.5 IIRC)。一些编译器使C和C++函数类型兼容,无论好坏,@ N.M:有趣。这是我从现在起必须记住的事情。我不知道有人这么做。非常感谢。