解决“const char[]类型没有+操作符“问题
Posted sanqima
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决“const char[]类型没有+操作符“问题相关的知识,希望对你有一定的参考价值。
今天,在MFC编程时,碰到一个字符串连接的报错问题,如图(1)所示:

出现该问题的原因: Unicode字符集对 CString字符串的+加号重载功能,支持不稳定,经常跳Bug;
解决方法:使用str.Format(“%s,%s”, strA,strB),格式化函数,间接实现字符串的相加。
1 代码A
//材料名称,材料单价
CString strPay = "insert into cailiaoinfo (materialName,materialPrice) values('"
+ m_strName + "'," + m_strPrice + ")";
2 代码B
CString strPay;
strPay.Format(_T("insert into cailiaoinfo (materialName,materialPrice) values('%s,%s)"),
m_strName, m_strPrice);
3 总结
代码A,仅在MFC的多字节字符集上使用;
代码B,既可以在多字节字符集上使用,也可以在Unicode字符集上使用。
效果如下

以上是关于解决“const char[]类型没有+操作符“问题的主要内容,如果未能解决你的问题,请参考以下文章