gpt4 book ai didi

C++ - strcpy_s 参数错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:49:14 25 4
gpt4 key购买 nike

我在使用 strcpy_s 时遇到一些错误,无法弄清楚我做错了什么。

代码:

播放器.hpp:

string name;
Player(string);

播放器.cpp:

Player::Player(string newName)
{
strcpy_s(name, name.size(), newName);//error is here
health = 20;
}

错误:

  • 函数调用中的参数过多
  • 没有重载函数“strcpy_s”的实例与参数列表匹配

最佳答案

您不能使用strcpy_s 来复制std::string。实际上,你只需要做:

Player::Player(string newName) {
name = newName;
health = 20;
}

更好的是,您可以使用 constructor initialization list :

Player::Player(string newName) : name(newName), health(20) {}

作为引用,这里有std::string的详细描述类。

关于C++ - strcpy_s 参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11478874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com