解決C/C++報錯error: cannot pass objects of non-trivially-copyable type ‘std::string’問題

1.問題描述

運行程序時,編譯報錯:

error: cannot pass objects of non-trivially-copyable type ‘std::string {aka struct std::basic_string}’ through ‘…’|

在這裏插入圖片描述

2.原因分析

報錯顯示在這一行:

printf("%c %s %lld %lld\n", p, edges, ver, edge);

其中edges爲string類型。

語法:

  • const char *c_str();
  • c_str()函數返回一個指向正規C字符串的指針, 內容與本string串相同.
  • 爲了與C兼容,在C中沒有string類型,故必須通過string類對象的成員函數c_str()把string對象轉換成C中的字符串樣式。

3.解決方法

在使用時,加入c_str();

printf("%c %s %lld %lld\n", p, edges.c_str(), ver, edge);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章