Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'

#include <stdio.h> #include <string> main() { int br_el[6],i; std::string qr_naziv[6];      qr_naziv[0]="Bath tub";      qr_naziv[1]="Sink";      qr_naziv[2]="Washing machine";      qr_naziv[3]="Toilet";      qr_naziv[4]="Kitchen sink";      qr_naziv[5]="Dish washer";   for(i=0;i<6;i++)     {          printf("Input the number for %s =",qr_naziv[i]);\\here lies the problem  scanf("%d",&br_el[i]); } 

This program is much longer, so I shortened it.. The thing is, I will enter numbers for array br_el[6], and I want it to show me for what object I am entering the number! So when I try to compile it gives me the error:"[Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string}' through '...'" I tried to declare string qr_naziv[6]; but the string didn't even bold so it didn't work, so I googled and found out another way (std::string qr_naziv[6];).

like image 217
Jovica96 Avatar asked May 03 '14 22:05

Jovica96


People also ask

Is STD string trivially copyable?

The diagnostic is not required, but std::atomic requires a trivially copyable type and std::string is not one.

What does trivially copyable mean?

A trivially copyable type is a type whose storage is contiguous (and thus its copy implies a trivial memory block copy, as if performed with memcpy), either cv-qualified or not. This is true for scalar types, trivially copyable classes and arrays of any such types.


1 Answers

Well, C functions are not acquainted with C++ structures. You should do the following:

... for(i = 0; i < 6; i++) {     printf("Input the number for %s =", qr_naziv[i].c_str());     scanf("%d", &br_el[i]); } ... 

Notice the call to the method c_str() on the each std::string qr_naziv[i], which returns a const char * to a null-terminated character array with data equivalent to those stored in the string -- a C-like string.

Edit: And, of course, since you're working with C++, the most appropriate to do is to use the stream operators insertion << and extraction >>, as duly noted by @MatsPetersson. In your case, you could do the following modification:

# include <iostream> ... for(i = 0; i < 6; i++) {     std::cout << "Input the number for " << qr_naziv[i] << " =";     std::cin >> br_el[i]; } ... 
like image 71
Rubens Avatar answered Oct 16 '22 00:10

Rubens