好久没写博客了,今儿就来水一篇hhh

学C++之前不是要先复习下C嘛,然后就从网上随便找了个项目敲一敲

我就不放源码了,大家应该都懂的,很简单的那种项目hhh

            case 4:cout << "请输入查找的名字" << endl;string new_name;cin >> new_name;break;case 0:        // <-----报错break;default:       // <-----报错break;

然后,此段代码报错了

F:\C++\txl\main.cpp:105: error:   crosses initialization of 'std::string new_name'string new_name;^

于是,我将那个从新定义字符串的代码扔到了`switch`外面,没再报错了,但我还是不知道这是为啥,google一下后

参考了之后:
https://stackoverflow.com/questions/42608101/crosses-initialization-of-string-and-jump-to-label-case

大佬的意思是

case labels are really a lot like dreaded goto labels; they do not constitute a new scope. Consequently, a switch selecting the correct case label to jump to is, for better or worse, a lot like goto statement jumping to a label. The jump is not allowed to cross the initialisation of your various objects - exactly as the error messages say.

简单说一下就是,switch 就是像是 goto 语句,他不会创建一个新scope(这个怎么翻译?大概是的意思?),在由goto关键词到label之间,创建变量是不被允许的

解决方式有两种:

  1. 要么将两个 case 之间的语句写到一个新函数里,在新函数那个局部区域里,创建变量是随意的
  2. 要么用大括号将那个代码段括起来 , 对,就是这个大括号 {}

error: crosses initialization of ‘std::string xx变量‘相关推荐

  1. error: invalid initialization of reference of type ‘std::string’ from expression of type ‘const s

    error: invalid initialization of reference of type 'std::string&' from expression of type 'const ...

  2. 【报错】关于[Error] cannot bind non-const lvalue reference of type ‘std::String‘ to an rvalue……的一个解决方案

    使用dev c++进行一波教科书代码的练习时出现了一点问题,于是来记录一波. 报错信息如下: [Error] cannot bind non-const lvalue reference of typ ...

  3. 【已解决】[Error] cannot pass objects of non-trivially-copyable type ‘std::string {aka class std::basic_s

    [Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string ...

  4. 解决C/C++报错error: cannot pass objects of non-trivially-copyable type ‘std::string’问题

    目录 1.问题描述 2.原因分析 3.解决方法 1.问题描述 运行程序时,编译报错: error: cannot pass objects of non-trivially-copyable type ...

  5. 关于std::string 在 并发场景下 __grow_by_and_replace free was not allocated 的异常问题

    使用string时发现了一些坑. 我们知道stl 容器并不是线程安全的,所以在使用它们的过程中往往需要一些同步机制来保证并发场景下的同步更新. 应该踩的坑还是一个不拉的踩了进去,所以还是记录一下吧. ...

  6. std string与线程安全_C++标准库多线程简介Part1

    Part1:线程与互斥量 本篇文章将简单的介绍一下C++的标准线程库,本篇内容十分基础,如果你有C++多线程相关的使用经验或者知识,就不必在这篇文章上浪费时间了... 如果你认为本篇文章对你有帮助,请 ...

  7. 【C++标准库】std::string用法指南源码剖析

    文章目录 1.ASCII码 (1)计算机如何表达字符 2.C 语言中的字符类型 char (1)思想:char 即整数 (3)C 语言帮手函数 (4)C语言中的字符串 (4)C 语言转义符 3.C++ ...

  8. ATL::CStringA和std::string之间转换的一些误区

    对于刚做windows下VC的开发同学,类型转换应该是一个令其很苦恼的问题.我刚写工作的时候,也为这类问题不停的在网上搜索转换方法.最近工作中遇到一个"神奇"的bug(一般&quo ...

  9. C++/C++11中std::string用法汇总

    C++/C++11中std::string是个模板类,它是一个标准库.使用string类型必须首先包含<string>头文件.作为标准库的一部分,string定义在命名空间std中. st ...

  10. 将整个ASCII文件读入C ++ std :: string [重复]

    本文翻译自:Read whole ASCII file into C++ std::string [duplicate] This question already has an answer her ...

最新文章

  1. 【C++】algorithm具体操作记录
  2. 【ACM】杭电OJ 2023
  3. [deviceone开发]-do_Http组件示例
  4. 解决IntelliJ IDEA无法读取配置文件的问题
  5. 小程序图片在安卓上拉伸的问题导航返回首页
  6. 童话镇计算机乐谱,童话镇简谱(歌词)-陈一发演唱-桃李醉春风记谱
  7. 实体类equals/hashCode
  8. 一种保护应用程序的方法 模拟Windows PE加载器,从内存资源中加载DLL
  9. oracle sql 查询无数据_信运大讲堂丨ORACLE数据库SQL和索引
  10. vue的万年历(日历)组件
  11. HTML框架分析及应用
  12. qq空间尾巴怎么修改成别的机型
  13. Win10如何为命令提示符增加输入命令获取管理员权限功能?
  14. SpringBoot使用LibreOffice word转换PDF
  15. JavaWeb 狂神
  16. python妹子图软件下载_python3下载妹子图,你懂的。
  17. 什么是哲学?《哲学家们都干了些什么?》读后感
  18. linux序列拼接软件,EST序列的聚类、拼接以及纠错是怎样进行的,推荐使用那些软件?!...
  19. 对软件公司不要传智播客学生的驳斥
  20. 基于51单片机的可调式电子仿真日历(时钟)

热门文章

  1. Adobe Acrobat Pro DC拆分PDF
  2. 《明朝那些事儿》读后感
  3. texture_laws 纹理滤波用于缺陷检测
  4. java x86 x64_x86 版和x64版有什么区别?
  5. ansible ---- 主机文件编写--在运行时显示自定义主机名称
  6. 万娟 白话大数据和机械学习_《白话大数据与机器学习》.pdf
  7. 微信服务号推送服务模板消息
  8. 一纬度横直线等于多公里_【归纳整理】高中地理必修一知识点总结
  9. csp是什么比赛_有关CSPJ/S比赛问题
  10. 2021年中国仪器仪表制造业经营现状分析:营业收入达9101.4亿元,利润总额达957亿元[图]