在运行代码之后,我得到了下面列出的错误。如何用strcpy_s替换strcpy?谢谢你的帮助。另外,当我尝试用strcpy_s替换strcpy时,它说命名空间"std“没有成员"strcpy_s”。
严重性代码描述项目文件行抑制状态错误C4996 'strcpy':此函数或变量可能不安全。考虑使用strcpy_s代替。若要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。有关详细信息,请参阅联机帮助。e:\nuitracksdk\nuitrack\include\nuitrack\types\issue.h 71 Nuitrack_project
严重性代码描述项目文件行抑制状态错误C4996 'strcpy':此函数或变量可能不安全。考虑使用strcpy_s代替。若要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。有关详细信息,请参阅联机帮助。e:\nuitracksdk\nuitrack\include\nuitrack\types\issue.h 81 Nuitrack_project
#ifndef NUITRACK_ISSUE_H_
#define NUITRACK_ISSUE_H_
#include <memory>
#include <string>
#include<cstring>
namespace tdv
{
namespace nuitrack
{
/**
* @ingroup CommonElements_group
* @brief Describes an issue identifier.
*/
enum IssueId
{
NONE_ISSUE = 0,
FRAME_BORDER_ISSUE = 1,
OCCLUSION_ISSUE = 2,
SENSOR_ISSUE = 3
};
/**
* @ingroup CommonElements_group
* @brief Stores general information about a issue.
*
* Parent class of all issue classes.
*/
class Issue
{
public:
/**
* @brief Smart pointer to access the Issue instance.
*/
typedef std::shared_ptr<Issue> Ptr;
/**
* @brief Returns the issue type as a string.
*/
static std::string getType()
{
static std::string _type = "Issue";
return _type;
}
/**
* @brief Returns the issue name.
*/
virtual std::string getName() const
{
return std::string(_name);
}
/**
* @brief Returns the issue identifier.
*/
IssueId getId()
{
return _id;
}
/**
* @brief Constructs a default issue.
*/
Issue() :
_id(NONE_ISSUE)
{
std::string name = "Issue";
_name = new char[name.length() + 1];
std::strcpy(_name, name.c_str());
}
/**
* @brief Constructs an issue object from its identifier and name.
*/
Issue(IssueId id, const std::string &name) :
_id(id)
{
_name = new char[name.length() + 1];
std::strcpy(_name, name.c_str());
}
virtual ~Issue()
{
deleteString();
}
/**
* Release the memory occupied by the string representation of the name.
*/
void deleteString()
{
if(_name)
{
delete[] _name;
_name = NULL;
}
}
/**
* @brief Copy constructor.
*/
Issue(const Issue& issue)
{
copyIssue(issue);
}
/**
* @brief Overloaded copy assignment operator.
*/
void operator=(const Issue& issue)
{
copyIssue(issue);
}
protected:
/** @warning For internal use only. */
IssueId _id;
/** @warning For internal use only. */
char* _name;
private:
void copyIssue(const Issue& issue)
{
_id = issue._id;
uint32_t nameSize = 0;
while(issue._name[nameSize] != '\0')
nameSize++;
_name = new char[nameSize + 1];
for(uint32_t i = 0; i <= nameSize; i++)
_name[i] = issue._name[i];
}
};
} /* namespace nuitrack */
} /* namespace tdv */
#endif /* NUITRACK_ISSUE_H_ */发布于 2019-01-06 00:01:00
strcpy_s ("s“表示”安全“)允许您指定目标缓冲区的大小。std::strcpy不会这样做,因此,如果源太长,就可以在目标缓冲区的末尾写入,这样效果就不好了。
在上面的第一个例子中,尝试如下:
_name = new char[name.length() + 1];
strcpy_s(_name, name.length() + 1, name.c_str());简单地用strcpy_s替换strcpy不起作用有两个原因: 1) strcpy_s接受另一个参数(目标缓冲区的长度) 2) strcpy_s不是std的一部分,即std名称空间不包含strcpy_s的声明
因此,尝试添加额外的参数,并将"std:strcpy“替换为"strcpy_s”
如果您在像Visual这样的IDE中,您可以经常右键单击"strcpy_s“这样的术语,从出现的上下文菜单中选择"Go to select”之类的内容,并获得有关该术语的一些快速信息。
注意:如果您是编译和构建C/C++程序的新手,那么也许有必要解释一下错误信息。该消息告诉您,如果您想使用传统的“不安全”c调用(如strcpy ),可以将_CRT_SECURE_NO_WARNINGS添加到构建系统中的预处理器定义中。这可以是包含相关标题之前的#define,也可以是IDE中的某个条目。如果输入该定义,编译器将允许您使用std::strcpy而不带怨言。
https://stackoverflow.com/questions/54057292
复制相似问题