文章目录

  • 前言
  • 一、发生的场景
  • 二、解决方法
    • 1.显式声明其中一个为std::string类型(推荐写法)
    • 2.去掉+,让编译器拼接字符串(不推荐)
  • 三、验证

前言

在使用C++中的字符串时,习惯性的把两个使用""括起来的字符串使用+连接,结果报错error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'
意思是对两个const char*类型的操作数进行+运算是非法的,本来想着两个字符串能够像c++中的string一样进行连接,结果与自己想的大相径庭。


一、发生的场景

 //std::string str = "hello" + "world"; // error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'//std::string str = std::string("hello") + " world"; // 正确//std::string str = "hello" + std::string("world"); //正确std::string str = "hello"" world"; //正确cout << "str=" << str << endl;

使用+运算符连接两个使用""包裹的字符串时报错。原因是在C++中,""括起来的字符串被当成是const char*类型,而非string类型。

二、解决方法

参考stackoverflow上的解释,详细内容请阅读error: invalid operands of types ‘const char*’ and ‘const char*’ to binary ‘operator+’

1.显式声明其中一个为std::string类型(推荐写法)

std::string str = std::string("hello") + " world";

2.去掉+,让编译器拼接字符串(不推荐)

std::string str = "hello"" world";

大多数编译器会自动拼接""包围的字符串,但不保证所有的编译器都正常。推荐显示声明为string类型的字符串再进行+操作。


三、验证

 std::string str1 = std::string("hello") + " world" + "!"; // 正确std::string str2 = "hello" + std::string(" world") + "!"; //正确std::string str3 = "hello"" world" "!"; //正确cout << "str1=" << str1 << endl;cout << "str2=" << str2 << endl;cout << "str3=" << str3 << endl;

解决error: invalid operands of types ‘const char [6]‘ and ‘const char [6]‘ to binary ‘operator+‘相关推荐

  1. [Error] invalid operands of types ‘float‘ and ‘float‘ to binary ‘operator%‘

    是因为求余运算符%要求左边的数是整数,但是把一个 float 类型的数放在%左边进行运算,就会报以上 错误 .所以要么转型(int)a,但是必须是a本身是整数的情况或者说符合题目要求只需要整型部分,不 ...

  2. gcc报invalid operands of types ‘<unresolved overloaded function type>‘ and ‘int‘ to binary ‘operator<

    会报这个错,基本上就是这么个情况. object->function<var>(par,par); 这么用的时候gcc把<var>这个尖括号的左边识别成了小于号. 解决也 ...

  3. error: invalid operands to binary % (have ‘double‘ and ‘int‘)取模mod %

    此处出现错误: 因为%不可以用于浮点型(double,float类型),只能用于整型(int类型) 可以强制类型转换,将浮点型强制转换为整型,解决此问题 double b = (int) floor( ...

  4. [error] MFC错误不能将参数1从const char [3]转换为const wchar_t *

    在做项目中经常遇到很多错误,这里我仅仅把自己遇到的一些错误和解决方法写出,供自己和大家查看. 代码如下: CRect rect; GetClientRect(&rect); CString s ...

  5. DX中关于error C2664: “ID3DXEffect::SetMatrix”: 不能将参数 1 从“const char [17]”转换为“D3DXHANDLE”

    用了这么久的DXUT,今天才发现对于shader中的变量无需先获取变量对于的handle然后再设置值,可以直接通过变量名来设置. // V( g_pEffect->SetMatrix( g_hW ...

  6. 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 ...

  7. ruby环境sass编译中文出现Syntax error: Invalid GBK character错误解决方法

    sass文件编译时候使用ruby环境,无论是界面化的koala工具还是命令行模式的都无法通过,真是令人烦恼. 容易出现中文注释时候无法编译通过,或者出现乱码,找了几天的解决方法终于解决了. 这个问题的 ...

  8. 成功解决Error:invalid character in identifier

    成功解决Error:invalid character in identifier 目录 解决问题 解决思路 解决方法 解决问题 解决思路 错误:标识符中的字符无效 解决方法 将单引号改为双引号即可! ...

  9. 成功解决./nvidia-installer: invalid option: ‐‐no‐opengl‐files ERROR: Invalid commandline, please run `

    成功解决./nvidia-installer: invalid option: "‐‐no‐opengl‐files" ERROR: Invalid commandline, pl ...

  10. Ubuntu开机出现“error: invalid environment block“的解决方法

    Ubuntu开机出现"error: invalid environment block"的解决方法 1. 错误状态 2. 解决办法 1. 错误状态 当使用GRUB启动ubuntu时 ...

最新文章

  1. plsql的安装与部署
  2. nginx.conf文件配置后访问边下载 以及yaf框架使用中NGINX.conf文件配置
  3. camel apache_Apache Camel 3的工作终于开始了
  4. 一个swiper 两个分页器的写法【总结】
  5. jsp 的 for循环
  6. python如何移动图片_python之详细图像仿射变换讲解(图像平移、旋转、缩放、翻转),一文就够了,赶紧码住...
  7. BAT大牛这样搞Python,真是绝了
  8. Linux性能及调优指南(翻译)
  9. Requirejs常用配置和应用
  10. Atitit zxing二维码qr码识别解析
  11. Atitit js版本es5 es6新特性
  12. python27缺少dll的解决
  13. web打印插件hiprint
  14. HTML5+CSS网页设计概述
  15. EAUML日拱一卒-活动图::14.2 Behavior StateMachines (6) - Transitions
  16. OC Runtime探索
  17. 智能运营助你轻松玩转消息推送
  18. 《算法分析与设计》练习6
  19. 如何使用开源CMS建设网站
  20. [转]rpcndr.h和wtypes.h冲突Bug的解决方案

热门文章

  1. mt4双线macd_ATFX:技术指标MACD及顶底背离用法精讲
  2. excel冻结窗口_冻结窗口怎么冻结多行
  3. 腾讯地图如何根据经纬度获取地址
  4. 由一道CTF pwn题深入理解libc2.26中的tcache机制
  5. GSM/CDMA区别
  6. Matlab中图像平移的实现
  7. PC端微信网页打不开
  8. 更好的为学计算机帮助英语,北京教委公布本科跨省转学名单,两学霸为学计算机转出北大...
  9. 提示Algorithm negotiation failed 错误该如何解决
  10. request请求头中出现Provisional headers are shown