簡體   English   中英

C ++-strcpy_s參數錯誤

[英]C++ - strcpy_s argument errors

使用strcpy_s時出現一些錯誤,無法弄清楚我在做什么錯。

編碼:

Player.hpp:

string name;
Player(string);

Player.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;
}

更好的是,您可以使用構造函數初始化列表

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

作為參考,這里有std :: string類的詳細描述。

此URL指出C ++版本僅使用模板重載函數(2個參數,而不是3個):

http://msdn.microsoft.com/zh-CN/library/td1esda9%28v=vs.80%29.aspx

模板errno_t strcpy_s(char(&strDestination)[size],const char * strSource); //僅C ++

根據這個URL:

在C ++中,模板重載簡化了這些功能的使用。 重載可以自動推斷緩沖區長度(無需指定大小參數),並且它們可以用較新的安全對等函數自動替換較舊的非安全函數。 有關更多信息,請參見安全模板重載。

(如原型中所述,此函數用於char *參數-不適用於字符串數據類型)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM