通过“...”错误]不能通过非平凡的,可复制的类型的对象'的std ::字符串{又名类的std :: basic_string的<&烧焦GT}“ [英] Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'

查看:2942
本文介绍了通过“...”错误]不能通过非平凡的,可复制的类型的对象'的std ::字符串{又名类的std :: basic_string的<&烧焦GT}“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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]);
}

本程序是更长的时间,所以我缩短了吧..
关键是,我会为阵列输入数字 br_el [6] ,我想它告诉我什么目标,我输入号码!
所以,当我尝试编译它给我的错误:[错误]不能穿过非平凡-复制的类型'的std ::字符串{又名类的std :: basic_string的}'对象'...'
我想声明字符串qr_naziv [6]; 但是字符串甚至没有加粗所以也没工作,所以我用Google搜索,发现了另一种方式(的std ::字符串qr_naziv [6];

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

推荐答案

好吧,C函数不使用C ++结构的认识。你应该做到以下几点:

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]);
}
...

注意调用方法 c_str() 对每个的std ::字符串qr_naziv [I] ,它返回的 为const char * 来等同于存储在字符串中一个空结束的字符数组数据的 - 类似C语言的字符串

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.

修改
而且,当然,因为你用C的工作++,最适合做的是使用流运营商插入&LT;&LT; 和提取 &GT;&GT; ,为正式通过@MatsPetersson指出。你的情况,你可以做以下修改:

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];
}
...

这篇关于通过“...”错误]不能通过非平凡的,可复制的类型的对象'的std ::字符串{又名类的std :: basic_string的&LT;&烧焦GT}“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆