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);