解决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);
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)