结构功能有问题

Trouble with strcpy function

本文关键字:有问题 功能 结构      更新时间:2023-10-16

我有一个用户定义的类,其中一个成员是char*类型。当我尝试在构造函数中初始化它时,我收到一条错误消息,指出error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.

但是,当我将strcpy更改为strcpy_s时,它仍然会给出以下错误IntelliSense: no instance of overloaded function "strcpy_s" matches the argument list argument types are: (char *, char *)

假设Student是类,char* name;是数据成员之一。所以,我的构造函数是这样的:

Student (char* s = NULL) {
    if (s != NULL) {
         name = new char[strlen(s) + 1];
         //strcpy(name,s);
         strcpy_s(name,s);
    }
}

这是因为strcpy_s需要一个额外的参数来指定要复制的字节数。

看这里: http://www.cplusplus.com/forum/beginner/118771/