运行程序时,编译报错:

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);
                    目录1.问题描述2.原因分析3.解决方法1.问题描述运行程序时,编译报错:error: cannot pass objects of non-trivially-copyable type ‘std::string {aka struct std::basic_string}’ through ‘…’|2.原因分析报错显示在这一行:printf("%c %s %lld %lld\...
				
It can be remarkably hard to design a good communications protocol, much harder even than it is to write a normal sequential program. Unfortunately, when the design of a new protocol is complete, we usually have little trouble convincing ourselves that it is trivially correct. It can be a unreasonably hard to prove those facts formally and to convince also others. Faced with that dilemma, a designer usually decides to trust his or her instincts and forgo the formal proofs. The subtle logical flaws in a design thus get a chance to hide, and inevitably find the worst possible moment in the lifetime of the protocol to reveal themselves. Though few will admit it, most people design protocols by trial and error. There is a known set of trusted protocol standards, whose descriptions are faithfully copied in most textbooks, but there is little understanding of why some designs are correct and why others are not. To design and to analyze protocols you need tools. Until recently the right tools were simply not generally available. But that has changed. In this tutorial we introduce a state-of-the-art tool called SPIN and a specification language called PROMELA, and we show how these can be used to design reliable protocols.
SYNOPSIS tcp source target DESCRIPTION The tcp utility copies the contents of the source to target. If target is a directory, tcp will copy source into this directory. EXAMPLES The following examples show common usage: tcp file1 file2 tcp file1 dir tcp file1 dir/file2 tcp file1 dir/subdir/subsubdir/file2 EXIT STATUS tcp exits 0 on success, and >0 if an error occurred. SEE ALSO tcpm(1), lseek(2), read(2), write(2) NOTES The code for the tcp utility is, of course, well-formatted and can be compiled using the -Wall flags without any errors or warnings. NAME tcpm—trivially copy a file via mmap/memcpy SYNOPSIS tcpm source target DESCRIPTION The tcpm utility copies the contents of the source to target. That is, behaves entirely the same as tcp(1). Unlike tcp(1), tcpm uses mmap(2) and memcpy(2) instead of read(2) and write(2), which is why it can be rewarded with up to 10 extra credit points. EXAMPLES The following examples show common usage: tcpm file1 file2 tcpm file1 dir tcpm file1 dir/file2 tcpm file1 dir/subdir/subsubdir/file2 EXIT STATUS tcpm exits 0 on success, and >0 if an error occurred. SEE ALSO tcp(1),lseek(2),mmap(2),memcpy(2) NOTES The code for the tcpm utility is, of course, well-formatted and can be compiled using the -Wall flags without any errors or warnings. 该存储库旨在提供一组出色的哈希映射实现,以及std :: map和std :: set的btree替代品,具有以下特征: 仅标头:无需构建,只需将parallel_hashmap目录复制到您的项目中就可以了。 替换std::unordered_map , std::unordered_set , std::map和std::set 需要具有C ++ 11支持的编译器,提供了C ++ 14和C ++ 17 API(例如try_emplace ) 非常高效,比编译器的无序映射/集合或Boost或显着更快 内存友好:内存使用率低,尽管比 支持异构查找 易于转发声明:只需在头文件中包含phmap_fwd_decl.h即可转发声明并行Hashmap容器[注意:当前不适用于带有指针键的哈希映射] 转储/加载功能:当flat哈希映射存储std::trivially_copya
Scanning dependencies of target algorithm [ 50%] Building CXX object CMakeFiles/aaa.cpp.o error: cannot pass non-trivial object of typestring’ (aka ’ basic_string< char, char_traits< char ...
[Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...' 1. 报错 [Error] cannot pass objects of non-trivially-copyable type 'std::...
ndk编译报错,是输出日志的错,  Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 14 in ./AndroidManifest.xml [armeabi-v7a] Compile++ thumb: SeetafaceSo <= Seetaface_JniClient.
具体可以看添加链接描述 但一开始我看了还是不太明白到底什么时候用。 后来想明白了,其实说白就是如果你要输出的字符串是string型,而不是字符数组型的,而你恰好又使用了printf来输出它,那么就要加上.c_str()。也就是说如果使用了cout来输出,就和平时一样了(测试了一下确实是这样)。 这就是为什么说.c_str()是为了与c兼容。 记录一下自己的问题
error: cannot pass objects of non-trivially-copyable typestd::string {aka class std::basic_string
报错cannot pass objects of non-trivially-copyable type 'std::string {aka struct std::basic_string}' through '...' 解决方法: 在使用时候 加入c_str()
[error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'
### 回答1: 这个错误提示是说不能通过省略号(ellipsis)传递非平凡复制类型(non-trivially-copyable type)的对象,其中的例子是字符串类 std::string(也被称为 std::basic_string<char>)。 简单来说,这个错误提示通常发生在函数定义或声明中,当你尝试通过省略号("...")来传递一个不支持复制或赋值操作的对象时会出现。这是因为省略号语法要求所有参数都支持复制操作,而某些类(如 std::string)并不支持这些操作。 要解决这个问题,你可以考虑使用指针或引用来传递对象,或者使用支持复制的类型来替换不支持复制的类型。如果你仍然需要传递非平凡复制类型的对象,你可以考虑使用其他的参数传递方式,如指定一个结构体或类作为参数,然后在该结构体或类中定义一个复制函数,以确保对象可以正确传递。 ### 回答2: 这个错误提示意味着在使用C++可变参数函数(va_args)时,不能传递非平凡可复制(non-trivially-copyable)类型的对象,特别是std::string这种字符串类。 这是由于非平凡可复制类型对象在复制时需要执行一些非平凡操作,如堆内存动态分配等。而va_args函数是没有办法知道如何正确复制这些对象的,因为它只知道传入的对象类型,而不知道对象的具体实现,因此会导致错误。 解决这个问题有两种方法:一种是使用平凡可复制类型的对象,如整数、浮点数、指针等等。另一种是使用指针或引用类型的对象,在调用时手动复制或转移所有权。 比如,在使用std::string时,可以使用const char*指针类型来替代string类型,并以参数方式传递地址。代码示例: void my_printf(const char* format, ...) va_list args; va_start(args, format); while (*format != '\0') { if (*format == '%') { format++; if (*format == 's') {//传递字符串 const char* str = va_arg(args, const char*); printf("%s", str); format++; va_end(args); string hello = "Hello World!"; my_printf("%s", hello.c_str());//使用const char*指针传递字符串 总之,在对非平凡可复制类型的对象进行复制时,一定要注意是否存在此类错误,并采取正确的解决方法。 ### 回答3: 这句话是C++中一个编译器报错信息,提示我们不能将一个非平凡复制类型的对象通过‘...’传递。简单来说,就是我们在使用可变参数的函数时,无法传递非平凡复制类型的对象。 平凡复制类型是指可以通过简单的内存复制进行复制的类型,比如int、double、char等基本数据类型以及一些类似于结构体的对象。而非平凡复制类型则是指不能简单地进行内存复制的类型,比如有指针、动态内存分配等特殊成员函数的类,比如std::string。 在C++中,可变参数通常使用‘...’表示,称为省略号参数。当我们定义可变参数函数时,需要在函数参数列表中使用省略号‘...’表示参数的个数未知。然而,由于可变参数传递是一种特殊的调用方式,编译器无法确定传递过来的非平凡复制类型的对象应该存储在哪里,也无法正确地释放它们。因此,编译器在编译过程中就主动报错,防止程序运行时出现不可预料的错误。 解决这个问题的方法很简单,就是避免将非平凡复制类型的对象通过可变参数传递。通常的做法是将需要传递的非平凡复制类型的对象改为引用或指针类型,并将它们放在可变参数之前的其他参数列表中传递。如果确实需要传递非平凡复制类型的对象,可以考虑使用序列化技术实现数据的保存和传输,或者使用特殊的技术来进行处理。
解决Springboot + MyBatis框架“Cause: org.springframework.jdbc.CannotGetJdbcConnectionException”问题 94735 解决Ubuntu18.04启动Docker“Got permission denied while trying to connect to the Docker daemon socket“问题 87830
解决C/C++报错error: cannot pass objects of non-trivially-copyable type ‘std::string’问题 一个小白编程爱好者: 应该是前面没有写const char *c_str(); Win10系统 MySQL & Navicat Premium12 安装教程 一生热爱7: 解压后只有一个navicat.exe的文件耶,其他没有咋整 在同一局域网下通过IP地址访问本机Tomcat项目 weixin_59754787: 你解决了么?急急急急啊 在同一局域网下通过IP地址访问本机Tomcat项目 weixin_45577253: 这样改localhost就登不了了 Win10环境Tomcat安装及环境变量配置教程 YCC_FY: 可行!感谢博主,之前一直弄免安装版本,运行startup直接闪退搞了很久也没有弄好