【原】error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'

2023-06-20,,

今天遇到一个非常难以排查的BUG,谷歌度娘都问过了依旧无解,最后自己重新尝试之后找到解决方案:

先看一下报错信息:

1>.\lenz.cpp(2197)  error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1> D:\Program Files\VC\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> D:\Program Files\VC\include\ostream(697): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,char)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
......//一行出错后就会有一堆这样的With[]....出现,非常烦

网上搜了一下,很多人说是因为没有#include <string>的缘故,但是反复确认后发现我有做到。百思不得其解,还一度以为是不同文件的头文件重复引用导致<string>失效,但是这条说不通!后面冷静了之后,双击错误信息查看源码,发现基本上这些错误含有两个特征:

1.都是LOG4CPLUS_DEBUG等调试函数

2.参数都有string类型

然后我单独写了一小段程序验证了一下,好家伙,真是这样子。原来,LOG4CPLUS_DEBUG函数不能直接打印string对象,只支持其转换形式.c_str()。

解决方法:把所有的LOG4CPLUS_DEBUG函数中的string加个c_str()

【原】error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'的相关教程结束。

《【原】error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'.doc》

下载本文的Word格式文档,以方便收藏与打印。